<!DOCTYPE html><html lang="zh-CN" data-theme="light"><head><div id="myscoll"></div><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no"><title>Python爬虫基本使用 | XiaoFei🥝</title><meta name="keywords" content="Python爬虫基本使用"><meta name="author" content="XiaoFei🥝"><meta name="copyright" content="XiaoFei🥝"><meta name="format-detection" content="telephone=no"><meta name="theme-color" content="ffffff"><meta name="description" content="Python爬虫基本使用">
<meta property="og:type" content="article">
<meta property="og:title" content="Python爬虫基本使用">
<meta property="og:url" content="https://www.naste.top:1024/posts/ad44f2a3.html">
<meta property="og:site_name" content="XiaoFei🥝">
<meta property="og:description" content="Python爬虫基本使用">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://www.naste.top:9000/webp/2514ce637e374a318653a40133bc66c5.webp">
<meta property="article:published_time" content="2022-06-22T10:01:02.000Z">
<meta property="article:modified_time" content="2024-04-26T09:52:19.212Z">
<meta property="article:author" content="XiaoFei🥝">
<meta property="article:tag" content="Python">
<meta property="article:tag" content="urllib">
<meta property="article:tag" content="Selenium">
<meta property="article:tag" content="requests">
<meta property="article:tag" content="scrapy">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://www.naste.top:9000/webp/2514ce637e374a318653a40133bc66c5.webp"><link rel="shortcut icon" href="/"><link rel="canonical" href="https://www.naste.top:1024/posts/ad44f2a3"><link rel="preconnect" href="//cdn.jsdelivr.net"/><link rel="preconnect" href="//busuanzi.ibruce.info"/><link rel="stylesheet" href="/css/index.css"><link rel="stylesheet" href="https://lf6-cdn-tos.bytecdntp.com/cdn/expire-1-M/font-awesome/6.0.0/css/all.min.css" media="print" onload="this.media='all'"><link rel="stylesheet" href="https://cdn.staticfile.org/fancyapps-ui/4.0.31/fancybox.min.css" media="print" onload="this.media='all'"><script>const GLOBAL_CONFIG = { 
  root: '/',
  algolia: undefined,
  localSearch: {"path":"/search.json","preload":true,"languages":{"hits_empty":"找不到您查询的内容：${query}"}},
  translate: undefined,
  noticeOutdate: {"limitDay":365,"position":"top","messagePrev":"It has been","messageNext":"days since the last update, the content of the article may be outdated."},
  highlight: {"plugin":"highlighjs","highlightCopy":true,"highlightLang":true,"highlightHeightLimit":230},
  copy: {
    success: '复制成功',
    error: '复制错误',
    noSupport: '浏览器不支持'
  },
  relativeDate: {
    homepage: true,
    post: true
  },
  runtime: '',
  date_suffix: {
    just: '刚刚',
    min: '分钟前',
    hour: '小时前',
    day: '天前',
    month: '个月前'
  },
  copyright: undefined,
  lightbox: 'fancybox',
  Snackbar: undefined,
  source: {
    justifiedGallery: {
      js: 'https://cdnjs.cloudflare.com/ajax/libs/flickr-justified-gallery/2.1.2/fjGallery.min.js',
      css: 'https://cdnjs.cloudflare.com/ajax/libs/flickr-justified-gallery/2.1.2/fjGallery.min.css'
    }
  },
  isPhotoFigcaption: false,
  islazyload: true,
  isAnchor: false
}</script><script id="config-diff">var GLOBAL_CONFIG_SITE = {
  title: 'Python爬虫基本使用',
  isPost: true,
  isHome: false,
  isHighlightShrink: false,
  isToc: true,
  postUpdate: '2024-04-26 17:52:19'
}</script><noscript><style type="text/css">
  #nav {
    opacity: 1
  }
  .justified-gallery img {
    opacity: 1
  }

  #recent-posts time,
  #post-meta time {
    display: inline !important
  }
</style></noscript><script>(win=>{
    win.saveToLocal = {
      set: function setWithExpiry(key, value, ttl) {
        if (ttl === 0) return
        const now = new Date()
        const expiryDay = ttl * 86400000
        const item = {
          value: value,
          expiry: now.getTime() + expiryDay,
        }
        localStorage.setItem(key, JSON.stringify(item))
      },

      get: function getWithExpiry(key) {
        const itemStr = localStorage.getItem(key)

        if (!itemStr) {
          return undefined
        }
        const item = JSON.parse(itemStr)
        const now = new Date()

        if (now.getTime() > item.expiry) {
          localStorage.removeItem(key)
          return undefined
        }
        return item.value
      }
    }
  
    win.getScript = url => new Promise((resolve, reject) => {
      const script = document.createElement('script')
      script.src = url
      script.async = true
      script.onerror = reject
      script.onload = script.onreadystatechange = function() {
        const loadState = this.readyState
        if (loadState && loadState !== 'loaded' && loadState !== 'complete') return
        script.onload = script.onreadystatechange = null
        resolve()
      }
      document.head.appendChild(script)
    })
  
      win.activateDarkMode = function () {
        document.documentElement.setAttribute('data-theme', 'dark')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#0d0d0d')
        }
      }
      win.activateLightMode = function () {
        document.documentElement.setAttribute('data-theme', 'light')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', 'ffffff')
        }
      }
      const t = saveToLocal.get('theme')
    
          const now = new Date()
          const hour = now.getHours()
          const isNight = hour <= 6 || hour >= 18
          if (t === undefined) isNight ? activateDarkMode() : activateLightMode()
          else if (t === 'light') activateLightMode()
          else activateDarkMode()
        
      const asideStatus = saveToLocal.get('aside-status')
      if (asideStatus !== undefined) {
        if (asideStatus === 'hide') {
          document.documentElement.classList.add('hide-aside')
        } else {
          document.documentElement.classList.remove('hide-aside')
        }
      }
    
    const detectApple = () => {
      if(/iPad|iPhone|iPod|Macintosh/.test(navigator.userAgent)){
        document.documentElement.classList.add('apple')
      }
    }
    detectApple()
    })(window)</script><link rel="stylesheet" href="https://cdn1.tianli0.top/npm/element-ui@2.15.6/packages/theme-chalk/lib/index.css"><style id="themeColor"></style><style id="rightSide"></style><style id="transPercent"></style><style id="blurNum"></style><style id="settingStyle"></style><span id="fps"></span><style id="defineBg"></style><style id="menu_shadow"></style><svg aria-hidden="true" style="position:absolute; overflow:hidden; width:0; height:0"><symbol id="icon-sun" viewBox="0 0 1024 1024"><path d="M960 512l-128 128v192h-192l-128 128-128-128H192v-192l-128-128 128-128V192h192l128-128 128 128h192v192z" fill="#FFD878" p-id="8420"></path><path d="M736 512a224 224 0 1 0-448 0 224 224 0 1 0 448 0z" fill="#FFE4A9" p-id="8421"></path><path d="M512 109.248L626.752 224H800v173.248L914.752 512 800 626.752V800h-173.248L512 914.752 397.248 800H224v-173.248L109.248 512 224 397.248V224h173.248L512 109.248M512 64l-128 128H192v192l-128 128 128 128v192h192l128 128 128-128h192v-192l128-128-128-128V192h-192l-128-128z" fill="#4D5152" p-id="8422"></path><path d="M512 320c105.888 0 192 86.112 192 192s-86.112 192-192 192-192-86.112-192-192 86.112-192 192-192m0-32a224 224 0 1 0 0 448 224 224 0 0 0 0-448z" fill="#4D5152" p-id="8423"></path></symbol><symbol id="icon-moon" viewBox="0 0 1024 1024"><path d="M611.370667 167.082667a445.013333 445.013333 0 0 1-38.4 161.834666 477.824 477.824 0 0 1-244.736 244.394667 445.141333 445.141333 0 0 1-161.109334 38.058667 85.077333 85.077333 0 0 0-65.066666 135.722666A462.08 462.08 0 1 0 747.093333 102.058667a85.077333 85.077333 0 0 0-135.722666 65.024z" fill="#FFB531" p-id="11345"></path><path d="M329.728 274.133333l35.157333-35.157333a21.333333 21.333333 0 1 0-30.165333-30.165333l-35.157333 35.157333-35.114667-35.157333a21.333333 21.333333 0 0 0-30.165333 30.165333l35.114666 35.157333-35.114666 35.157334a21.333333 21.333333 0 1 0 30.165333 30.165333l35.114667-35.157333 35.157333 35.157333a21.333333 21.333333 0 1 0 30.165333-30.165333z" fill="#030835" p-id="11346"></path></symbol></svg><!-- hexo injector head_end start --><link rel="stylesheet" href="https://npm.elemecdn.com/hexo-butterfly-swiper/lib/swiper.min.css" media="print" onload="this.media='all'"><link rel="stylesheet" href="https://npm.elemecdn.com/hexo-butterfly-swiper/lib/swiperstyle.css" media="print" onload="this.media='all'"><link rel="stylesheet" href="https://npm.elemecdn.com/hexo-butterfly-tag-plugins-plus@latest/lib/assets/font-awesome-animation.min.css" media="defer" onload="this.media='all'"><link rel="stylesheet" href="https://npm.elemecdn.com/hexo-butterfly-tag-plugins-plus@latest/lib/tag_plugins.css" media="defer" onload="this.media='all'"><script src="https://npm.elemecdn.com/hexo-butterfly-tag-plugins-plus@latest/lib/assets/carousel-touch.js"></script><link rel="stylesheet" href="https://npm.elemecdn.com/hexo-butterfly-wowjs/lib/animate.min.css" media="print" onload="this.media='screen'"><link rel="stylesheet" href="https://npm.elemecdn.com/hexo-filter-gitcalendar/lib/gitcalendar.css" media="print" onload="this.media='all'"><!-- hexo injector head_end end --><meta name="generator" content="Hexo 6.3.0"><link rel="alternate" href="/atom.xml" title="XiaoFei🥝" type="application/atom+xml">
</head><body><div id="loading-box" onclick="document.getElementById(&quot;loading-box&quot;).classList.add(&quot;loaded&quot;)"><div class="loading-bg"><div class="loading-img"></div><div class="loading-image-dot"></div></div></div><div id="web_bg"></div><div id="sidebar"><div id="menu-mask"></div><div id="sidebar-menus"><div class="avatar-img is-center"><img src= "" data-lazy-src="https://www.naste.top:9000/images/cat.jpg" onerror="onerror=null;src='/assets/r1.jpg'" alt="avatar"/></div><div class="sidebar-site-data site-data is-center"><a href="/archives/"><div class="headline">文章</div><div class="length-num">149</div></a><a href="/tags/"><div class="headline">标签</div><div class="length-num">99</div></a><a href="/categories/"><div class="headline">分类</div><div class="length-num">46</div></a></div><hr/><div class="menus_items"><div class="menus_item"><a class="site-page faa-parent animated-hover" href="/"><svg class="menu_icon faa-tada" aria-hidden="true" style="width:1.30em;height:1.30em;vertical-align:-0.15em;fill:currentColor;overflow:hidden;"><use xlink:href="#icon-home"></use></svg><span class="menu_word" style="font-size:17px"> 首页</span></a></div><div class="menus_item"><a class="site-page group faa-parent animated-hover hide" href="javascript:void(0);"><svg class="menu_icon faa-tada" aria-hidden="true" style="width:1.30em;height:1.30em;vertical-align:-0.15em;fill:currentColor;overflow:hidden;"><use xlink:href="#icon--article"></use></svg><span class="menu_word" style="font-size:17px"> 文章</span><i class="fas fa-chevron-down"></i></a><ul class="menus_item_child"><li><a class="site-page child faa-parent animated-hover" href="/archives/"><svg class="menu_icon faa-tada" aria-hidden="true" style="width:1.30em;height:1.30em;vertical-align:-0.15em;fill:currentColor;overflow:hidden;"><use xlink:href="#icon-guidang1">                   </use></svg><span class="menu_word" style="font-size:17px"> 归档</span></a></li><li><a class="site-page child faa-parent animated-hover" href="/tags/"><svg class="menu_icon faa-tada" aria-hidden="true" style="width:1.30em;height:1.30em;vertical-align:-0.15em;fill:currentColor;overflow:hidden;"><use xlink:href="#icon-sekuaibiaoqian">                   </use></svg><span class="menu_word" style="font-size:17px"> 标签</span></a></li><li><a class="site-page child faa-parent animated-hover" href="/categories/"><svg class="menu_icon faa-tada" aria-hidden="true" style="width:1.30em;height:1.30em;vertical-align:-0.15em;fill:currentColor;overflow:hidden;"><use xlink:href="#icon-fenlei">                   </use></svg><span class="menu_word" style="font-size:17px"> 分类</span></a></li></ul></div><div class="menus_item"><a class="site-page group faa-parent animated-hover hide" href="javascript:void(0);"><svg class="menu_icon faa-tada" aria-hidden="true" style="width:1.30em;height:1.30em;vertical-align:-0.15em;fill:currentColor;overflow:hidden;"><use xlink:href="#icon-pinweishenghuo"></use></svg><span class="menu_word" style="font-size:17px"> 休闲</span><i class="fas fa-chevron-down"></i></a><ul class="menus_item_child"><li><a class="site-page child faa-parent animated-hover" href="/life/music/"><svg class="menu_icon faa-tada" aria-hidden="true" style="width:1.30em;height:1.30em;vertical-align:-0.15em;fill:currentColor;overflow:hidden;"><use xlink:href="#icon-yinle">                   </use></svg><span class="menu_word" style="font-size:17px"> 八音盒</span></a></li><li><a class="site-page child faa-parent animated-hover" href="/life/movies/"><svg class="menu_icon faa-tada" aria-hidden="true" style="width:1.30em;height:1.30em;vertical-align:-0.15em;fill:currentColor;overflow:hidden;"><use xlink:href="#icon-dianying1">                   </use></svg><span class="menu_word" style="font-size:17px"> 影院</span></a></li><li><a class="site-page child faa-parent animated-hover" href="/life/games/"><svg class="menu_icon faa-tada" aria-hidden="true" style="width:1.30em;height:1.30em;vertical-align:-0.15em;fill:currentColor;overflow:hidden;"><use xlink:href="#icon-youxishoubing">                   </use></svg><span class="menu_word" style="font-size:17px"> 游戏</span></a></li></ul></div><div class="menus_item"><a class="site-page group faa-parent animated-hover hide" href="javascript:void(0);"><svg class="menu_icon faa-tada" aria-hidden="true" style="width:1.30em;height:1.30em;vertical-align:-0.15em;fill:currentColor;overflow:hidden;"><use xlink:href="#icon-xiangzi"></use></svg><span class="menu_word" style="font-size:17px"> 八宝箱</span><i class="fas fa-chevron-down"></i></a><ul class="menus_item_child"><li><a class="site-page child faa-parent animated-hover" href="/box/gallery/"><svg class="menu_icon faa-tada" aria-hidden="true" style="width:1.30em;height:1.30em;vertical-align:-0.15em;fill:currentColor;overflow:hidden;"><use xlink:href="#icon-tubiaozhizuomoban">                   </use></svg><span class="menu_word" style="font-size:17px"> 画廊</span></a></li><li><a class="site-page child faa-parent animated-hover" href="/box/animation/"><svg class="menu_icon faa-tada" aria-hidden="true" style="width:1.30em;height:1.30em;vertical-align:-0.15em;fill:currentColor;overflow:hidden;"><use xlink:href="#icon-nvwumao">                   </use></svg><span class="menu_word" style="font-size:17px"> 动画</span></a></li><li><a class="site-page child faa-parent animated-hover" href="/box/nav/"><svg class="menu_icon faa-tada" aria-hidden="true" style="width:1.30em;height:1.30em;vertical-align:-0.15em;fill:currentColor;overflow:hidden;"><use xlink:href="#icon-zhifengche">                   </use></svg><span class="menu_word" style="font-size:17px"> 网址导航</span></a></li></ul></div><div class="menus_item"><a class="site-page group faa-parent animated-hover hide" href="javascript:void(0);"><svg class="menu_icon faa-tada" aria-hidden="true" style="width:1.30em;height:1.30em;vertical-align:-0.15em;fill:currentColor;overflow:hidden;"><use xlink:href="#icon-shejiaoxinxi"></use></svg><span class="menu_word" style="font-size:17px"> 社交</span><i class="fas fa-chevron-down"></i></a><ul class="menus_item_child"><li><a class="site-page child faa-parent animated-hover" href="/social/fcircle/"><svg class="menu_icon faa-tada" aria-hidden="true" style="width:1.30em;height:1.30em;vertical-align:-0.15em;fill:currentColor;overflow:hidden;"><use xlink:href="#icon-pengyouquan">                   </use></svg><span class="menu_word" style="font-size:17px"> 朋友圈</span></a></li><li><a class="site-page child faa-parent animated-hover" href="/comments/"><svg class="menu_icon faa-tada" aria-hidden="true" style="width:1.30em;height:1.30em;vertical-align:-0.15em;fill:currentColor;overflow:hidden;"><use xlink:href="#icon-liuyan">                   </use></svg><span class="menu_word" style="font-size:17px"> 留言板</span></a></li><li><a class="site-page child faa-parent animated-hover" href="/social/link/"><svg class="menu_icon faa-tada" aria-hidden="true" style="width:1.30em;height:1.30em;vertical-align:-0.15em;fill:currentColor;overflow:hidden;"><use xlink:href="#icon-lianjie">                   </use></svg><span class="menu_word" style="font-size:17px"> 友人帐</span></a></li></ul></div><div class="menus_item"><a class="site-page group faa-parent animated-hover hide" href="javascript:void(0);"><svg class="menu_icon faa-tada" aria-hidden="true" style="width:1.30em;height:1.30em;vertical-align:-0.15em;fill:currentColor;overflow:hidden;"><use xlink:href="#icon-wangye"></use></svg><span class="menu_word" style="font-size:17px"> 网站</span><i class="fas fa-chevron-down"></i></a><ul class="menus_item_child"><li><a class="site-page child faa-parent animated-hover" href="/site/census/"><svg class="menu_icon faa-tada" aria-hidden="true" style="width:1.30em;height:1.30em;vertical-align:-0.15em;fill:currentColor;overflow:hidden;"><use xlink:href="#icon--tongjibiao">                   </use></svg><span class="menu_word" style="font-size:17px"> 网站统计</span></a></li><li><a class="site-page child faa-parent animated-hover" href="/site/echarts/"><svg class="menu_icon faa-tada" aria-hidden="true" style="width:1.30em;height:1.30em;vertical-align:-0.15em;fill:currentColor;overflow:hidden;"><use xlink:href="#icon-shujutongji1">                   </use></svg><span class="menu_word" style="font-size:17px"> 文章统计</span></a></li><li><a class="site-page child faa-parent animated-hover" href="/site/time/"><svg class="menu_icon faa-tada" aria-hidden="true" style="width:1.30em;height:1.30em;vertical-align:-0.15em;fill:currentColor;overflow:hidden;"><use xlink:href="#icon-xianxingshalou">                   </use></svg><span class="menu_word" style="font-size:17px"> 旧时光</span></a></li></ul></div><div class="menus_item"><a class="site-page group faa-parent animated-hover hide" href="javascript:void(0);"><svg class="menu_icon faa-tada" aria-hidden="true" style="width:1.30em;height:1.30em;vertical-align:-0.15em;fill:currentColor;overflow:hidden;"><use xlink:href="#icon-maoliang"></use></svg><span class="menu_word" style="font-size:17px"> 个人</span><i class="fas fa-chevron-down"></i></a><ul class="menus_item_child"><li><a class="site-page child faa-parent animated-hover" href="/personal/bb/"><svg class="menu_icon faa-tada" aria-hidden="true" style="width:1.30em;height:1.30em;vertical-align:-0.15em;fill:currentColor;overflow:hidden;"><use xlink:href="#icon-qunliaotian">                   </use></svg><span class="menu_word" style="font-size:17px"> 唠叨</span></a></li><li><a class="site-page child faa-parent animated-hover" href="/personal/love/"><svg class="menu_icon faa-tada" aria-hidden="true" style="width:1.30em;height:1.30em;vertical-align:-0.15em;fill:currentColor;overflow:hidden;"><use xlink:href="#icon-love-sign">                   </use></svg><span class="menu_word" style="font-size:17px"> 恋爱小屋</span></a></li><li><a class="site-page child faa-parent animated-hover" href="/personal/about/"><svg class="menu_icon faa-tada" aria-hidden="true" style="width:1.30em;height:1.30em;vertical-align:-0.15em;fill:currentColor;overflow:hidden;"><use xlink:href="#icon-paperplane">                   </use></svg><span class="menu_word" style="font-size:17px"> 关于</span></a></li><li><a class="site-page child faa-parent animated-hover" href="/encryption/%E6%9C%BA%E5%9C%BA/"><svg class="menu_icon faa-tada" aria-hidden="true" style="width:1.30em;height:1.30em;vertical-align:-0.15em;fill:currentColor;overflow:hidden;"><use xlink:href="#icon-paperplane">                   </use></svg><span class="menu_word" style="font-size:17px"> 机场</span></a></li></ul></div></div></div></div><div class="post" id="body-wrap"><header class="post-bg" id="page-header"><nav id="nav"><span id="blog_name"><a id="site-name" href="/">XiaoFei🥝</a></span><div id="menus"><div class="menus_items"><div class="menus_item"><a class="site-page faa-parent animated-hover" href="/"><svg class="menu_icon faa-tada" aria-hidden="true" style="width:1.30em;height:1.30em;vertical-align:-0.15em;fill:currentColor;overflow:hidden;"><use xlink:href="#icon-home"></use></svg><span class="menu_word" style="font-size:17px"> 首页</span></a></div><div class="menus_item"><a class="site-page group faa-parent animated-hover hide" href="javascript:void(0);"><svg class="menu_icon faa-tada" aria-hidden="true" style="width:1.30em;height:1.30em;vertical-align:-0.15em;fill:currentColor;overflow:hidden;"><use xlink:href="#icon--article"></use></svg><span class="menu_word" style="font-size:17px"> 文章</span><i class="fas fa-chevron-down"></i></a><ul class="menus_item_child"><li><a class="site-page child faa-parent animated-hover" href="/archives/"><svg class="menu_icon faa-tada" aria-hidden="true" style="width:1.30em;height:1.30em;vertical-align:-0.15em;fill:currentColor;overflow:hidden;"><use xlink:href="#icon-guidang1">                   </use></svg><span class="menu_word" style="font-size:17px"> 归档</span></a></li><li><a class="site-page child faa-parent animated-hover" href="/tags/"><svg class="menu_icon faa-tada" aria-hidden="true" style="width:1.30em;height:1.30em;vertical-align:-0.15em;fill:currentColor;overflow:hidden;"><use xlink:href="#icon-sekuaibiaoqian">                   </use></svg><span class="menu_word" style="font-size:17px"> 标签</span></a></li><li><a class="site-page child faa-parent animated-hover" href="/categories/"><svg class="menu_icon faa-tada" aria-hidden="true" style="width:1.30em;height:1.30em;vertical-align:-0.15em;fill:currentColor;overflow:hidden;"><use xlink:href="#icon-fenlei">                   </use></svg><span class="menu_word" style="font-size:17px"> 分类</span></a></li></ul></div><div class="menus_item"><a class="site-page group faa-parent animated-hover hide" href="javascript:void(0);"><svg class="menu_icon faa-tada" aria-hidden="true" style="width:1.30em;height:1.30em;vertical-align:-0.15em;fill:currentColor;overflow:hidden;"><use xlink:href="#icon-pinweishenghuo"></use></svg><span class="menu_word" style="font-size:17px"> 休闲</span><i class="fas fa-chevron-down"></i></a><ul class="menus_item_child"><li><a class="site-page child faa-parent animated-hover" href="/life/music/"><svg class="menu_icon faa-tada" aria-hidden="true" style="width:1.30em;height:1.30em;vertical-align:-0.15em;fill:currentColor;overflow:hidden;"><use xlink:href="#icon-yinle">                   </use></svg><span class="menu_word" style="font-size:17px"> 八音盒</span></a></li><li><a class="site-page child faa-parent animated-hover" href="/life/movies/"><svg class="menu_icon faa-tada" aria-hidden="true" style="width:1.30em;height:1.30em;vertical-align:-0.15em;fill:currentColor;overflow:hidden;"><use xlink:href="#icon-dianying1">                   </use></svg><span class="menu_word" style="font-size:17px"> 影院</span></a></li><li><a class="site-page child faa-parent animated-hover" href="/life/games/"><svg class="menu_icon faa-tada" aria-hidden="true" style="width:1.30em;height:1.30em;vertical-align:-0.15em;fill:currentColor;overflow:hidden;"><use xlink:href="#icon-youxishoubing">                   </use></svg><span class="menu_word" style="font-size:17px"> 游戏</span></a></li></ul></div><div class="menus_item"><a class="site-page group faa-parent animated-hover hide" href="javascript:void(0);"><svg class="menu_icon faa-tada" aria-hidden="true" style="width:1.30em;height:1.30em;vertical-align:-0.15em;fill:currentColor;overflow:hidden;"><use xlink:href="#icon-xiangzi"></use></svg><span class="menu_word" style="font-size:17px"> 八宝箱</span><i class="fas fa-chevron-down"></i></a><ul class="menus_item_child"><li><a class="site-page child faa-parent animated-hover" href="/box/gallery/"><svg class="menu_icon faa-tada" aria-hidden="true" style="width:1.30em;height:1.30em;vertical-align:-0.15em;fill:currentColor;overflow:hidden;"><use xlink:href="#icon-tubiaozhizuomoban">                   </use></svg><span class="menu_word" style="font-size:17px"> 画廊</span></a></li><li><a class="site-page child faa-parent animated-hover" href="/box/animation/"><svg class="menu_icon faa-tada" aria-hidden="true" style="width:1.30em;height:1.30em;vertical-align:-0.15em;fill:currentColor;overflow:hidden;"><use xlink:href="#icon-nvwumao">                   </use></svg><span class="menu_word" style="font-size:17px"> 动画</span></a></li><li><a class="site-page child faa-parent animated-hover" href="/box/nav/"><svg class="menu_icon faa-tada" aria-hidden="true" style="width:1.30em;height:1.30em;vertical-align:-0.15em;fill:currentColor;overflow:hidden;"><use xlink:href="#icon-zhifengche">                   </use></svg><span class="menu_word" style="font-size:17px"> 网址导航</span></a></li></ul></div><div class="menus_item"><a class="site-page group faa-parent animated-hover hide" href="javascript:void(0);"><svg class="menu_icon faa-tada" aria-hidden="true" style="width:1.30em;height:1.30em;vertical-align:-0.15em;fill:currentColor;overflow:hidden;"><use xlink:href="#icon-shejiaoxinxi"></use></svg><span class="menu_word" style="font-size:17px"> 社交</span><i class="fas fa-chevron-down"></i></a><ul class="menus_item_child"><li><a class="site-page child faa-parent animated-hover" href="/social/fcircle/"><svg class="menu_icon faa-tada" aria-hidden="true" style="width:1.30em;height:1.30em;vertical-align:-0.15em;fill:currentColor;overflow:hidden;"><use xlink:href="#icon-pengyouquan">                   </use></svg><span class="menu_word" style="font-size:17px"> 朋友圈</span></a></li><li><a class="site-page child faa-parent animated-hover" href="/comments/"><svg class="menu_icon faa-tada" aria-hidden="true" style="width:1.30em;height:1.30em;vertical-align:-0.15em;fill:currentColor;overflow:hidden;"><use xlink:href="#icon-liuyan">                   </use></svg><span class="menu_word" style="font-size:17px"> 留言板</span></a></li><li><a class="site-page child faa-parent animated-hover" href="/social/link/"><svg class="menu_icon faa-tada" aria-hidden="true" style="width:1.30em;height:1.30em;vertical-align:-0.15em;fill:currentColor;overflow:hidden;"><use xlink:href="#icon-lianjie">                   </use></svg><span class="menu_word" style="font-size:17px"> 友人帐</span></a></li></ul></div><div class="menus_item"><a class="site-page group faa-parent animated-hover hide" href="javascript:void(0);"><svg class="menu_icon faa-tada" aria-hidden="true" style="width:1.30em;height:1.30em;vertical-align:-0.15em;fill:currentColor;overflow:hidden;"><use xlink:href="#icon-wangye"></use></svg><span class="menu_word" style="font-size:17px"> 网站</span><i class="fas fa-chevron-down"></i></a><ul class="menus_item_child"><li><a class="site-page child faa-parent animated-hover" href="/site/census/"><svg class="menu_icon faa-tada" aria-hidden="true" style="width:1.30em;height:1.30em;vertical-align:-0.15em;fill:currentColor;overflow:hidden;"><use xlink:href="#icon--tongjibiao">                   </use></svg><span class="menu_word" style="font-size:17px"> 网站统计</span></a></li><li><a class="site-page child faa-parent animated-hover" href="/site/echarts/"><svg class="menu_icon faa-tada" aria-hidden="true" style="width:1.30em;height:1.30em;vertical-align:-0.15em;fill:currentColor;overflow:hidden;"><use xlink:href="#icon-shujutongji1">                   </use></svg><span class="menu_word" style="font-size:17px"> 文章统计</span></a></li><li><a class="site-page child faa-parent animated-hover" href="/site/time/"><svg class="menu_icon faa-tada" aria-hidden="true" style="width:1.30em;height:1.30em;vertical-align:-0.15em;fill:currentColor;overflow:hidden;"><use xlink:href="#icon-xianxingshalou">                   </use></svg><span class="menu_word" style="font-size:17px"> 旧时光</span></a></li></ul></div><div class="menus_item"><a class="site-page group faa-parent animated-hover hide" href="javascript:void(0);"><svg class="menu_icon faa-tada" aria-hidden="true" style="width:1.30em;height:1.30em;vertical-align:-0.15em;fill:currentColor;overflow:hidden;"><use xlink:href="#icon-maoliang"></use></svg><span class="menu_word" style="font-size:17px"> 个人</span><i class="fas fa-chevron-down"></i></a><ul class="menus_item_child"><li><a class="site-page child faa-parent animated-hover" href="/personal/bb/"><svg class="menu_icon faa-tada" aria-hidden="true" style="width:1.30em;height:1.30em;vertical-align:-0.15em;fill:currentColor;overflow:hidden;"><use xlink:href="#icon-qunliaotian">                   </use></svg><span class="menu_word" style="font-size:17px"> 唠叨</span></a></li><li><a class="site-page child faa-parent animated-hover" href="/personal/love/"><svg class="menu_icon faa-tada" aria-hidden="true" style="width:1.30em;height:1.30em;vertical-align:-0.15em;fill:currentColor;overflow:hidden;"><use xlink:href="#icon-love-sign">                   </use></svg><span class="menu_word" style="font-size:17px"> 恋爱小屋</span></a></li><li><a class="site-page child faa-parent animated-hover" href="/personal/about/"><svg class="menu_icon faa-tada" aria-hidden="true" style="width:1.30em;height:1.30em;vertical-align:-0.15em;fill:currentColor;overflow:hidden;"><use xlink:href="#icon-paperplane">                   </use></svg><span class="menu_word" style="font-size:17px"> 关于</span></a></li><li><a class="site-page child faa-parent animated-hover" href="/encryption/%E6%9C%BA%E5%9C%BA/"><svg class="menu_icon faa-tada" aria-hidden="true" style="width:1.30em;height:1.30em;vertical-align:-0.15em;fill:currentColor;overflow:hidden;"><use xlink:href="#icon-paperplane">                   </use></svg><span class="menu_word" style="font-size:17px"> 机场</span></a></li></ul></div></div><center id="name-container"><a id="page-name" href="javascript:scrollToTop()">PAGE_NAME</a></center><div id="nav-right"><div id="search-button"><a class="search faa-parent animated-hover" title="检索站内任何你想要的信息"><svg class="faa-tada icon" style="height:24px;width:24px;fill:currentColor;position:relative;top:6px" aria-hidden="true"><use xlink:href="#icon-valentine_-search-love-find-heart"></use></svg><span> 搜索</span></a></div><a class="meihua faa-parent animated-hover" onclick="toggleWinbox()" title="美化设置-自定义你的风格" id="meihua-button"><svg class="faa-tada icon" style="height:26px;width:26px;fill:currentColor;position:relative;top:8px" aria-hidden="true"><use xlink:href="#icon-tupian1"></use></svg></a><a class="sun_moon faa-parent animated-hover" onclick="switchNightMode()" title="浅色和深色模式转换" id="nightmode-button"><svg class="faa-tada" style="height:25px;width:25px;fill:currentColor;position:relative;top:7px" viewBox="0 0 1024 1024"><use id="modeicon" xlink:href="#icon-moon">       </use></svg></a><div id="toggle-menu"><a><i class="fas fa-bars fa-fw"></i></a></div></div></div></nav><div id="post-info"><h1 class="post-title">Python爬虫基本使用</h1><div id="post-meta"><div class="meta-firstline"><span class="post-meta-date"><svg class="meta_icon post-meta-icon" style="width:30px;height:30px;position:relative;top:10px"><use xlink:href="#icon-rili"></use></svg><span class="post-meta-label">发表于 </span><time class="post-meta-date-created" datetime="2022-06-22T10:01:02.000Z" title="发表于 2022-06-22 18:01:02">2022-06-22</time><span class="post-meta-separator">|</span><svg class="meta_icon post-meta-icon" style="width:18px;height:18px;position:relative;top:5px"><use xlink:href="#icon-gengxin1"></use></svg><span class="post-meta-label">更新于</span><time class="post-meta-date-updated" datetime="2024-04-26T09:52:19.212Z" title="更新于 2024-04-26 17:52:19">2024-04-26</time></span><span class="post-meta-categories"><span class="post-meta-separator">|</span><svg class="meta_icon post-meta-icon" style="width:18px;height:18px;position:relative;top:5px"><use xlink:href="#icon-biaoqian"></use></svg><a class="post-meta-categories" href="/categories/Python/">Python</a><i class="fas fa-angle-right post-meta-separator"></i><svg class="meta_icon post-meta-icon" style="width:18px;height:18px;position:relative;top:5px"><use xlink:href="#icon-biaoqian"></use></svg><a class="post-meta-categories" href="/categories/Python/%E7%88%AC%E8%99%AB/">爬虫</a></span></div><div class="meta-secondline"><span class="post-meta-separator">|</span><span class="post-meta-wordcount"><svg class="meta_icon post-meta-icon" style="width:25px;height:25px;position:relative;top:8px"><use xlink:href="#icon-charuword"></use></svg><span class="post-meta-label">字数总计:</span><span class="word-count">1.4w</span><span class="post-meta-separator">|</span><svg class="meta_icon post-meta-icon" style="width:20px;height:20px;position:relative;top:5px"><use xlink:href="#icon-shizhong"></use></svg><span class="post-meta-label">阅读时长:</span><span>59分钟</span></span><span class="post-meta-separator">|</span><span class="post-meta-pv-cv" id="" data-flag-title="Python爬虫基本使用"><svg class="meta_icon post-meta-icon" style="width:25px;height:25px;position:relative;top:5px"><use xlink:href="#icon-eye"></use></svg><span class="post-meta-label">阅读量:</span><span id="busuanzi_value_page_pv"><i class="fa-solid fa-spinner fa-spin"></i></span></span></div></div></div><section class="main-hero-waves-area waves-area"><svg class="waves-svg" xmlns="http://www.w3.org/2000/svg" xlink="http://www.w3.org/1999/xlink" viewBox="0 24 150 28" preserveAspectRatio="none" shape-rendering="auto"><defs><path id="gentle-wave" d="M -160 44 c 30 0 58 -18 88 -18 s 58 18 88 18 s 58 -18 88 -18 s 58 18 88 18 v 44 h -352 Z"></path></defs><g class="parallax"><use href="#gentle-wave" x="48" y="0"></use><use href="#gentle-wave" x="48" y="3"></use><use href="#gentle-wave" x="48" y="5"></use><use href="#gentle-wave" x="48" y="7"></use></g></svg></section></header><main class="layout" id="content-inner"><div id="post"><article class="post-content" id="article-container"><h1 id="一、python基础"><a href="#一、python基础" class="headerlink" title="一、python基础"></a>一、python基础</h1><h2 id="1、注释"><a href="#1、注释" class="headerlink" title="1、注释"></a>1、注释</h2><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 单行注释</span></span><br><span class="line"></span><br><span class="line"><span class="string">&quot;&quot;&quot;</span></span><br><span class="line"><span class="string">多行注释</span></span><br><span class="line"><span class="string">&quot;&quot;&quot;</span></span><br><span class="line"></span><br><span class="line"><span class="string">&#x27;&#x27;&#x27;</span></span><br><span class="line"><span class="string">多行注释</span></span><br><span class="line"><span class="string">&#x27;&#x27;&#x27;</span></span><br></pre></td></tr></table></figure>
<h2 id="2、变量以及数据类型"><a href="#2、变量以及数据类型" class="headerlink" title="2、变量以及数据类型"></a>2、变量以及数据类型</h2><p><strong>在python中，不需要定义变量的类型，系统会自动根据变量的值来确定变量的类型</strong></p>
<p><strong>【List（列表）———Java中的数组】【Tuple（元组）———】</strong></p>
<p><img src= "" data-lazy-src="https://www.naste.top:9000/webp/image-20210902114204961.webp" alt="image-20210902114204961"></p>
<h3 id="2-1、变量定义"><a href="#2-1、变量定义" class="headerlink" title="2.1、变量定义"></a>2.1、变量定义</h3><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">#数字类型【 int 和 float 】</span></span><br><span class="line">a = <span class="number">123</span></span><br><span class="line">b = <span class="number">12.8</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># 布尔类型</span></span><br><span class="line">c = <span class="literal">False</span></span><br><span class="line">d = <span class="literal">True</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># 字符串类型</span></span><br><span class="line">e = <span class="string">&quot;今天天气真好&quot;</span></span><br><span class="line">f = <span class="string">&#x27;今天天气不好&#x27;</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># List列表类型【相当于Java中的Object数组或集合】</span></span><br><span class="line">g = [<span class="number">1</span>, <span class="string">&quot;字符串2&quot;</span>, <span class="number">3</span>, <span class="string">&#x27;字符串4&#x27;</span>]</span><br><span class="line"></span><br><span class="line"><span class="comment"># Tuple（元组）【相当于Java中的Object数组或集合】</span></span><br><span class="line">h = (<span class="number">1</span>, <span class="string">&quot;字符串2&quot;</span>, <span class="number">3</span>, <span class="string">&#x27;字符串4&#x27;</span>)</span><br><span class="line"></span><br><span class="line"><span class="comment"># Dictionary（字典）【相当于Java中的Map集合】</span></span><br><span class="line">i = &#123;<span class="string">&#x27;age&#x27;</span>: <span class="number">18</span>, <span class="string">&#x27;name&#x27;</span>: <span class="string">&#x27;张三&#x27;</span>&#125;</span><br></pre></td></tr></table></figure>
<h3 id="2-2、查看变量的数据类型"><a href="#2-2、查看变量的数据类型" class="headerlink" title="2.2、查看变量的数据类型"></a>2.2、查看变量的数据类型</h3><p><strong>使用<code>type</code>来查看变量的数据类型</strong></p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">a = <span class="number">123</span></span><br><span class="line"><span class="built_in">print</span>(<span class="built_in">type</span>(a))</span><br></pre></td></tr></table></figure>
<h2 id="3、类型转换"><a href="#3、类型转换" class="headerlink" title="3、类型转换"></a>3、类型转换</h2><div class="table-container">
<table>
<thead>
<tr>
<th style="text-align:center">函数</th>
<th style="text-align:center">说明</th>
</tr>
</thead>
<tbody>
<tr>
<td style="text-align:center">int(x)</td>
<td style="text-align:center">将x转换为一个整数</td>
</tr>
<tr>
<td style="text-align:center">float(x)</td>
<td style="text-align:center">将x转换为一个浮点数</td>
</tr>
<tr>
<td style="text-align:center">str(x)</td>
<td style="text-align:center">将对象 x 转换为字符串</td>
</tr>
<tr>
<td style="text-align:center">bool(x)</td>
<td style="text-align:center">将对象x转换成为布尔值</td>
</tr>
</tbody>
</table>
</div>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 以下情况转换为bool类型全为false</span></span><br><span class="line"><span class="built_in">print</span>(<span class="built_in">bool</span>(<span class="string">&#x27;&#x27;</span>))</span><br><span class="line"><span class="built_in">print</span>(<span class="built_in">bool</span>(<span class="string">&quot;&quot;</span>))</span><br><span class="line"><span class="built_in">print</span>(<span class="built_in">bool</span>(<span class="number">0</span>))</span><br><span class="line"><span class="built_in">print</span>(<span class="built_in">bool</span>(&#123;&#125;))</span><br><span class="line"><span class="built_in">print</span>(<span class="built_in">bool</span>([]))</span><br><span class="line"><span class="built_in">print</span>(<span class="built_in">bool</span>(()))</span><br></pre></td></tr></table></figure>
<h2 id="4、运算符"><a href="#4、运算符" class="headerlink" title="4、运算符"></a>4、运算符</h2><h3 id="4-1、算数运算符"><a href="#4-1、算数运算符" class="headerlink" title="4.1、算数运算符"></a>4.1、算数运算符</h3><p><strong>下面以a=10 ,b=20为例进行计算</strong></p>
<div class="table-container">
<table>
<thead>
<tr>
<th style="text-align:center">运算符</th>
<th style="text-align:center">描述</th>
<th style="text-align:center">实例</th>
</tr>
</thead>
<tbody>
<tr>
<td style="text-align:center">+</td>
<td style="text-align:center">加</td>
<td style="text-align:center">两个对象相加 a + b 输出结果 30</td>
</tr>
<tr>
<td style="text-align:center">-</td>
<td style="text-align:center">减</td>
<td style="text-align:center">得到负数或是一个数减去另一个数 a - b 输出结果 -10</td>
</tr>
<tr>
<td style="text-align:center">*</td>
<td style="text-align:center">乘</td>
<td style="text-align:center">两个数相乘或是返回一个被重复若干次的字符串 a * b 输出结果 200</td>
</tr>
<tr>
<td style="text-align:center">/</td>
<td style="text-align:center">除</td>
<td style="text-align:center">b / a 输出结果 2</td>
</tr>
<tr>
<td style="text-align:center">//</td>
<td style="text-align:center">取整数</td>
<td style="text-align:center">返回商的整数部分 9//2 输出结果 4 , 9.0//2.0 输出结果 4.0</td>
</tr>
<tr>
<td style="text-align:center">%</td>
<td style="text-align:center">取余</td>
<td style="text-align:center">返回除法的余数 b % a 输出结果 0</td>
</tr>
<tr>
<td style="text-align:center">**</td>
<td style="text-align:center">指数</td>
<td style="text-align:center">a**b 为10的20次方</td>
</tr>
<tr>
<td style="text-align:center">()</td>
<td style="text-align:center">小括号</td>
<td style="text-align:center">提高运算优先级，比如: (1+2) * 3</td>
</tr>
</tbody>
</table>
</div>
<p>==注意：混合运算时，优先级顺序为： <em>* 高于 </em> / % // 高于 + - ，为了避免歧义，建议使用 () 来处理运 算符优先级。 并且，不同类型的数字在进行混合运算时，整数将会转换成浮点数进行运算。在字符串中使用<code>+</code>是字符串拼接==</p>
<h3 id="4-2、赋值运算符"><a href="#4-2、赋值运算符" class="headerlink" title="4.2、赋值运算符"></a>4.2、赋值运算符</h3><div class="table-container">
<table>
<thead>
<tr>
<th style="text-align:center">运算符</th>
<th style="text-align:center">描述</th>
<th style="text-align:center">实例</th>
</tr>
</thead>
<tbody>
<tr>
<td style="text-align:center">=</td>
<td style="text-align:center">赋值运算符</td>
<td style="text-align:center">把 = 号右边的结果 赋给 左边的变量，如 num = 1 + 2 * 3，结果num的值为7</td>
</tr>
</tbody>
</table>
</div>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">a = <span class="number">10</span></span><br><span class="line">b = c = <span class="number">2</span></span><br><span class="line"><span class="comment"># 以上两种赋值方式在其他编程语言中都能实现，下面这种方式是python中特有的</span></span><br><span class="line">d, e, f = <span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span></span><br></pre></td></tr></table></figure>
<h3 id="4-3、复合赋值运算符"><a href="#4-3、复合赋值运算符" class="headerlink" title="4.3、复合赋值运算符"></a>4.3、复合赋值运算符</h3><div class="table-container">
<table>
<thead>
<tr>
<th style="text-align:center">运算符</th>
<th style="text-align:center">描述</th>
<th style="text-align:center">实例</th>
</tr>
</thead>
<tbody>
<tr>
<td style="text-align:center">+=</td>
<td style="text-align:center">加法赋值运算符</td>
<td style="text-align:center">c += a 等效于 c = c + a</td>
</tr>
<tr>
<td style="text-align:center">-=</td>
<td style="text-align:center">减法赋值运算符</td>
<td style="text-align:center">c -= a 等效于 c = c - a</td>
</tr>
<tr>
<td style="text-align:center">*=</td>
<td style="text-align:center">乘法赋值运算符</td>
<td style="text-align:center">c <em>= a 等效于 c = c </em> a</td>
</tr>
<tr>
<td style="text-align:center">/=</td>
<td style="text-align:center">除法赋值运算符</td>
<td style="text-align:center">c /= a 等效于 c = c / a</td>
</tr>
<tr>
<td style="text-align:center">//=</td>
<td style="text-align:center">取整除赋值运算符</td>
<td style="text-align:center">c //= a 等效于 c = c // a</td>
</tr>
<tr>
<td style="text-align:center">%=</td>
<td style="text-align:center">取模赋值运算符</td>
<td style="text-align:center">c %= a 等效于 c = c % a</td>
</tr>
<tr>
<td style="text-align:center">**=</td>
<td style="text-align:center">幂赋值运算符</td>
<td style="text-align:center">c = a 等效于 c = c ** a</td>
</tr>
</tbody>
</table>
</div>
<h3 id="4-4、比较运算符"><a href="#4-4、比较运算符" class="headerlink" title="4.4、比较运算符"></a>4.4、比较运算符</h3><div class="table-container">
<table>
<thead>
<tr>
<th style="text-align:center">运算符</th>
<th style="text-align:center">描述</th>
<th style="text-align:center">实例</th>
</tr>
</thead>
<tbody>
<tr>
<td style="text-align:center">==</td>
<td style="text-align:center">等于:比较对象是否相等</td>
<td style="text-align:center">(a == b) 返回 False</td>
</tr>
<tr>
<td style="text-align:center">!=</td>
<td style="text-align:center">不等于:比较两个对象是否不相等</td>
<td style="text-align:center">(a != b) 返回 true</td>
</tr>
<tr>
<td style="text-align:center">&gt;</td>
<td style="text-align:center">大于:返回x是否大于y</td>
<td style="text-align:center">(a &gt; b) 返回 False</td>
</tr>
<tr>
<td style="text-align:center">&gt;=</td>
<td style="text-align:center">大于等于:返回x是否大于等于y</td>
<td style="text-align:center">(a &gt;= b) 返回 False</td>
</tr>
<tr>
<td style="text-align:center">&lt;</td>
<td style="text-align:center">小于:返回x是否小于y。所有比较运算符返回1表示真，返回0表示假。这分别与特 殊的变量True和False等价</td>
<td style="text-align:center">(a &lt; b) 返回 true</td>
</tr>
<tr>
<td style="text-align:center">&lt;=</td>
<td style="text-align:center">小于等于:返回x是否小于等于y</td>
<td style="text-align:center">(a &lt;= b) 返回 true</td>
</tr>
</tbody>
</table>
</div>
<h3 id="4-5、逻辑运算符"><a href="#4-5、逻辑运算符" class="headerlink" title="4.5、逻辑运算符"></a>4.5、逻辑运算符</h3><div class="table-container">
<table>
<thead>
<tr>
<th style="text-align:center">运算符</th>
<th style="text-align:center">逻辑表达式</th>
<th style="text-align:center">描述</th>
<th style="text-align:center">实例</th>
</tr>
</thead>
<tbody>
<tr>
<td style="text-align:center">and</td>
<td style="text-align:center">x and y</td>
<td style="text-align:center">只要有一个运算数是False，结果就是False; 只有所有的运算数都为True时，结果才是True 做取值运算时，取第一个为False的值，如果所有的值 都为True,取最后一个值</td>
<td style="text-align:center">True and True and False—&gt; 结果为False True and True and True—&gt; 结果为True</td>
</tr>
<tr>
<td style="text-align:center">or</td>
<td style="text-align:center">x or y</td>
<td style="text-align:center">只要有一个运算数是True，结果就是True; 只有所有的运算数都为False时，结果才是False 做取值运算时，取第一个为True的值，如果所有的值都 为False,取最后一个值</td>
<td style="text-align:center">False or False or True—&gt;结 果为True False or False or False—&gt;结 果为False</td>
</tr>
<tr>
<td style="text-align:center">not</td>
<td style="text-align:center">not x</td>
<td style="text-align:center">布尔”非” - 如果 x 为 True，返回 False 。如果 x 为 False，它返回 True。</td>
<td style="text-align:center">not True —&gt; False</td>
</tr>
</tbody>
</table>
</div>
<blockquote>
<p><strong>性能优化</strong></p>
</blockquote>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">a = <span class="number">11</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># and的性能优化,当前的结果是False的情况下,那么后面的代码就不再执行了</span></span><br><span class="line">a &gt; <span class="number">10</span> <span class="keyword">and</span> <span class="built_in">print</span>(<span class="string">&#x27;会输出&#x27;</span>)</span><br><span class="line"></span><br><span class="line"><span class="comment"># or的性能优化,当前面的结果是True的情况下,后面的代码就不再执行了</span></span><br><span class="line">a == <span class="number">11</span> <span class="keyword">or</span> <span class="built_in">print</span>(<span class="string">&#x27;不会输出&#x27;</span>)</span><br></pre></td></tr></table></figure>
<h2 id="5、输入和输出"><a href="#5、输入和输出" class="headerlink" title="5、输入和输出"></a>5、输入和输出</h2><h3 id="5-1、输出"><a href="#5-1、输出" class="headerlink" title="5.1、输出"></a>5.1、输出</h3><blockquote>
<p><strong>普通输出</strong></p>
</blockquote>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">print</span>(<span class="string">&#x27;aaaa&#x27;</span>)</span><br><span class="line"></span><br><span class="line">a = <span class="string">&quot;这是拼接后的字符串&quot;</span></span><br><span class="line"><span class="built_in">print</span>(<span class="string">&quot;字符串1&quot;</span> + a)</span><br></pre></td></tr></table></figure>
<blockquote>
<p><strong>格式化输出</strong></p>
</blockquote>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">age = <span class="number">10</span></span><br><span class="line"><span class="built_in">print</span>(<span class="string">&quot;我今年%d岁&quot;</span> % age)</span><br><span class="line"></span><br><span class="line">name = <span class="string">&quot;红浪漫晶哥&quot;</span></span><br><span class="line"><span class="built_in">print</span>(<span class="string">&quot;我的姓名是%s, 年龄是%d&quot;</span> % (name, age))</span><br></pre></td></tr></table></figure>
<h3 id="5-2、输入"><a href="#5-2、输入" class="headerlink" title="5.2、输入"></a>5.2、输入</h3><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">password = <span class="built_in">input</span>(<span class="string">&#x27;请输入密码：&#x27;</span>)</span><br><span class="line"><span class="built_in">print</span>(<span class="string">&#x27;输入的密码为：%s&#x27;</span> % password)</span><br></pre></td></tr></table></figure>
<h2 id="6、流程控制语句"><a href="#6、流程控制语句" class="headerlink" title="6、流程控制语句"></a>6、流程控制语句</h2><h3 id="6-1、if"><a href="#6-1、if" class="headerlink" title="6.1、if"></a>6.1、if</h3><blockquote>
<p><strong>格式</strong></p>
</blockquote>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">if</span> 要判断的条件:</span><br><span class="line">    条件成立时，要做的事情</span><br></pre></td></tr></table></figure>
<blockquote>
<p><strong>示例</strong></p>
</blockquote>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">a = <span class="number">10</span></span><br><span class="line"><span class="keyword">if</span> a &gt; <span class="number">5</span>:</span><br><span class="line">    <span class="built_in">print</span>(<span class="string">&#x27;条件成立，输出了&#x27;</span>)</span><br></pre></td></tr></table></figure>
<hr>
<h3 id="6-2、if-else"><a href="#6-2、if-else" class="headerlink" title="6.2、if-else"></a>6.2、if-else</h3><blockquote>
<p><strong>格式</strong></p>
</blockquote>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">if</span> 要判断的条件:</span><br><span class="line">    条件成立时，执行的操作</span><br><span class="line"><span class="keyword">else</span>:</span><br><span class="line">    条件不成立时，候执行的操作</span><br></pre></td></tr></table></figure>
<blockquote>
<p><strong>示例</strong></p>
</blockquote>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">a = <span class="number">10</span></span><br><span class="line"><span class="keyword">if</span> a &gt; <span class="number">0</span>:</span><br><span class="line">    <span class="built_in">print</span>(<span class="string">&quot;条件成立&quot;</span>)</span><br><span class="line"><span class="keyword">else</span>:</span><br><span class="line">    <span class="built_in">print</span>(<span class="string">&quot;条件不成立&quot;</span>)</span><br></pre></td></tr></table></figure>
<hr>
<h3 id="6-3、elif"><a href="#6-3、elif" class="headerlink" title="6.3、elif"></a>6.3、elif</h3><blockquote>
<p><strong>格式</strong></p>
</blockquote>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">a = <span class="number">10</span></span><br><span class="line"><span class="keyword">if</span> 判断的条件:</span><br><span class="line">    条件成立时，执行的操作</span><br><span class="line"><span class="keyword">elif</span> 判断的条件:</span><br><span class="line">    条件成立时，执行的操作</span><br><span class="line"><span class="keyword">else</span>:</span><br><span class="line">    条件成立时，执行的操作</span><br></pre></td></tr></table></figure>
<blockquote>
<p><strong>示例</strong></p>
</blockquote>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">a = <span class="number">10</span></span><br><span class="line"><span class="keyword">if</span> a &gt; <span class="number">0</span>:</span><br><span class="line">    <span class="built_in">print</span>(<span class="string">&quot;a&gt;0&quot;</span>)</span><br><span class="line"><span class="keyword">elif</span> a &lt; -<span class="number">10</span>:</span><br><span class="line">    <span class="built_in">print</span>(<span class="string">&#x27;a&lt;-10&#x27;</span>)</span><br><span class="line"><span class="keyword">else</span>:</span><br><span class="line">    <span class="built_in">print</span>(<span class="string">&#x27;其他条件&#x27;</span>)</span><br></pre></td></tr></table></figure>
<hr>
<h3 id="6-4、for"><a href="#6-4、for" class="headerlink" title="6.4、for"></a>6.4、for</h3><blockquote>
<p><strong>格式</strong></p>
</blockquote>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">for</span> 临时变量 <span class="keyword">in</span> 列表或者字符串等可迭代对象:</span><br><span class="line">	循环满足条件时执行的代码</span><br></pre></td></tr></table></figure>
<blockquote>
<p><strong>示例</strong></p>
</blockquote>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 循环字符串</span></span><br><span class="line"><span class="keyword">for</span> s <span class="keyword">in</span> <span class="string">&quot;hello&quot;</span>:</span><br><span class="line">	<span class="built_in">print</span>(s)</span><br><span class="line"><span class="comment"># 遍历列表</span></span><br><span class="line">items = [<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>]</span><br><span class="line"><span class="keyword">for</span> item <span class="keyword">in</span> items:</span><br><span class="line">    <span class="built_in">print</span>(item)</span><br></pre></td></tr></table></figure>
<hr>
<h3 id="6-5、range"><a href="#6-5、range" class="headerlink" title="6.5、range"></a>6.5、range</h3><p><strong>range可以生成一个列表用于循环，它可以传递三个参数，分别表示 起始、结束和步长</strong></p>
<blockquote>
<p><strong>示例</strong></p>
</blockquote>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">for</span> item <span class="keyword">in</span> <span class="built_in">range</span>(<span class="number">1</span>, <span class="number">10</span>, <span class="number">3</span>):</span><br><span class="line">    <span class="built_in">print</span>(item)</span><br><span class="line"><span class="comment"># 输出：1，4，7</span></span><br></pre></td></tr></table></figure>
<h2 id="7、数据类型高级"><a href="#7、数据类型高级" class="headerlink" title="7、数据类型高级"></a>7、数据类型高级</h2><h3 id="7-1、字符串高级"><a href="#7-1、字符串高级" class="headerlink" title="7.1、字符串高级"></a>7.1、字符串高级</h3><ul>
<li>len：获取字符串的长度</li>
<li>find：查找指定内容在字符串中是否存在，不存在返回-1，存在返回第一次出现的位置</li>
<li>startswith    endswith：判断以谁开头/结尾，返回bool值</li>
<li>count：返回指定内容在字符串中出现的次数</li>
<li>replace：替换内容，两个参数，第一个，替换前的值，第二个，替换后的值</li>
<li>split：切割字符串，会将切割后的结果返回为列表</li>
<li>upper    lower：upper（小写全部变为大写，大写不变），lower（大写变为小写，小写不变）</li>
<li>strip：去除字符串的前后空格，中间的空格不会去除</li>
<li>join：字符串拼接，和正常的拼接不一样 ，测试一下就知道了</li>
</ul>
<h3 id="7-2、列表高级"><a href="#7-2、列表高级" class="headerlink" title="7.2、列表高级"></a>7.2、列表高级</h3><h4 id="7-2-1、列表添加"><a href="#7-2-1、列表添加" class="headerlink" title="7.2.1、列表添加"></a>7.2.1、列表添加</h4><ul>
<li>append 在末尾添加元素 ，会修改原来的数组</li>
<li>insert 在指定位置插入元素  ，会修改原来的数组</li>
<li>extend 合并两个列表 ，会修改原来的数组</li>
</ul>
<blockquote>
<p><strong>示例</strong></p>
</blockquote>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line">a = [<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>]</span><br><span class="line">a.append(<span class="number">4</span>)</span><br><span class="line"><span class="built_in">print</span>(a)  <span class="comment"># [1, 2, 3, 4]</span></span><br><span class="line"></span><br><span class="line">a.insert(<span class="number">11</span>, <span class="number">11</span>)</span><br><span class="line"><span class="built_in">print</span>(a)  <span class="comment"># [1, 2, 3, 4, 11]</span></span><br><span class="line"></span><br><span class="line">b = [<span class="number">22</span>, <span class="number">33</span>]</span><br><span class="line">a.extend(b)</span><br><span class="line"><span class="built_in">print</span>(a)  <span class="comment"># [1, 2, 3, 4, 11, 22, 33]</span></span><br></pre></td></tr></table></figure>
<h4 id="7-2-2、列表修改"><a href="#7-2-2、列表修改" class="headerlink" title="7.2.2、列表修改"></a>7.2.2、列表修改</h4><p><strong>找到指定元素的下标重新赋值</strong></p>
<blockquote>
<p><strong>示例</strong></p>
</blockquote>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">a = [<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>]</span><br><span class="line">a[<span class="number">1</span>] = <span class="number">231313</span></span><br><span class="line"><span class="built_in">print</span>(a)  <span class="comment"># [1, 231313, 3]</span></span><br></pre></td></tr></table></figure>
<h4 id="7-2-3、列表查询"><a href="#7-2-3、列表查询" class="headerlink" title="7.2.3、列表查询"></a>7.2.3、列表查询</h4><ul>
<li>in（存在）,如果存在那么结果为true，否则为false </li>
<li>not in（不存在），如果不存在那么结果为true，否则false</li>
</ul>
<blockquote>
<p><strong>格式</strong></p>
</blockquote>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">print</span>(判断的值 <span class="keyword">in</span> / <span class="keyword">not</span> <span class="keyword">in</span> 列表)</span><br></pre></td></tr></table></figure>
<blockquote>
<p><strong>示例</strong></p>
</blockquote>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">a = [<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>]</span><br><span class="line"><span class="built_in">print</span>(<span class="number">1</span> <span class="keyword">in</span> a)  <span class="comment"># True</span></span><br><span class="line"><span class="built_in">print</span>(<span class="number">11</span> <span class="keyword">in</span> a)  <span class="comment"># False</span></span><br></pre></td></tr></table></figure>
<h4 id="7-2-4、列表删除"><a href="#7-2-4、列表删除" class="headerlink" title="7.2.4、列表删除"></a>7.2.4、列表删除</h4><ul>
<li>del：根据下标进行删除 </li>
<li>pop：删除最后一个元素 </li>
<li>remove：根据元素的值进行删除</li>
</ul>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line">a = [<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>]</span><br><span class="line"><span class="keyword">del</span> a[<span class="number">1</span>]</span><br><span class="line"><span class="built_in">print</span>(a)  <span class="comment"># [1, 3, 4]</span></span><br><span class="line"></span><br><span class="line">a = [<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>]</span><br><span class="line">a.pop()</span><br><span class="line"><span class="built_in">print</span>(a)  <span class="comment"># [1, 2, 3]</span></span><br><span class="line"></span><br><span class="line">a = [<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>]</span><br><span class="line">a.remove(<span class="number">1</span>)</span><br><span class="line"><span class="built_in">print</span>(a)  <span class="comment"># [2, 3, 4]</span></span><br></pre></td></tr></table></figure>
<h3 id="7-3、元组高级"><a href="#7-3、元组高级" class="headerlink" title="7.3、元组高级"></a>7.3、元组高级</h3><p>==<strong>Python的元组与列表类似，不同之处在于元组的元素不能修改。元组使用小括号，列表使用方括号。</strong>==</p>
<p><strong>==当元组中只有一个元素的时候，必须在最后一个元素后面添加一个，==</strong></p>
<h4 id="7-3-1、元组查询"><a href="#7-3-1、元组查询" class="headerlink" title="7.3.1、元组查询"></a>7.3.1、元组查询</h4><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">a = (<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>)</span><br><span class="line"><span class="built_in">print</span>(a[<span class="number">1</span>])  <span class="comment"># 2</span></span><br></pre></td></tr></table></figure>
<h3 id="7-4、切片"><a href="#7-4、切片" class="headerlink" title="7.4、切片"></a>7.4、切片</h3><p><strong>切片就是截取，截取部分字符串，获取元组或列表中的部分数据，字符串，列表，元组都支持切片</strong></p>
<blockquote>
<p><strong>格式</strong></p>
</blockquote>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">切片的语法：[起始:结束:步长]，也可以简化使用 [起始:结束]</span><br><span class="line">注意：选取的区间从<span class="string">&quot;起始&quot;</span>位开始，到<span class="string">&quot;结束&quot;</span>位的前一位结束（不包含结束位本身)，步长表示选取间隔</span><br></pre></td></tr></table></figure>
<blockquote>
<p><strong>示例</strong></p>
</blockquote>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">s = <span class="string">&#x27;Hello World!&#x27;</span></span><br><span class="line"><span class="built_in">print</span>(s)</span><br><span class="line"><span class="built_in">print</span>(s[<span class="number">4</span>])  <span class="comment"># 获取指定位置的值，输出：o</span></span><br><span class="line"><span class="built_in">print</span>(s[<span class="number">3</span>:<span class="number">7</span>])  <span class="comment"># 从第几个位置开始截取，截取到第几个，输出：lo W</span></span><br><span class="line"><span class="built_in">print</span>(s[<span class="number">5</span>:])  <span class="comment"># 从第几个位置开始截取，截取到最后一个，输出： World!</span></span><br><span class="line"><span class="built_in">print</span>(s[:<span class="number">5</span>])  <span class="comment"># 从起始位置开始，取到 下标为4的前一个元素（不包括结束位本身），输出：Hello</span></span><br><span class="line"><span class="built_in">print</span>(s[<span class="number">1</span>:<span class="number">5</span>:<span class="number">2</span>])  <span class="comment"># 从下标为1开始，取到下标为5的前一个元素，步长为2（不包括结束位本身）输出：el</span></span><br></pre></td></tr></table></figure>
<h3 id="7-5、字典高级"><a href="#7-5、字典高级" class="headerlink" title="7.5、字典高级"></a>7.5、字典高级</h3><h4 id="7-5-1、字典查询"><a href="#7-5-1、字典查询" class="headerlink" title="7.5.1、字典查询"></a>7.5.1、字典查询</h4><p><strong>除了使用key查找数据，还可以使用get来获取数据</strong></p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">person = &#123;<span class="string">&#x27;name&#x27;</span>: <span class="string">&#x27;韩信&#x27;</span>, <span class="string">&#x27;age&#x27;</span>: <span class="number">18</span>, <span class="string">&#x27;gender&#x27;</span>: <span class="string">&#x27;男&#x27;</span>&#125;</span><br><span class="line"></span><br><span class="line"><span class="built_in">print</span>(person.get(<span class="string">&#x27;name&#x27;</span>))  <span class="comment"># 输出：韩信</span></span><br><span class="line"><span class="built_in">print</span>(person.get(<span class="string">&#x27;error&#x27;</span>))  <span class="comment"># 不报错。输出：None</span></span><br><span class="line"></span><br><span class="line"><span class="built_in">print</span>(person[<span class="string">&#x27;gender&#x27;</span>])  <span class="comment"># 当键不存在的时候，会报错。输出：男</span></span><br><span class="line"><span class="built_in">print</span>(person[<span class="string">&#x27;error&#x27;</span>])  <span class="comment"># 报 KeyError 错误</span></span><br></pre></td></tr></table></figure>
<h4 id="7-5-2、字典修改"><a href="#7-5-2、字典修改" class="headerlink" title="7.5.2、字典修改"></a>7.5.2、字典修改</h4><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">person = &#123;<span class="string">&#x27;name&#x27;</span>: <span class="string">&#x27;韩信&#x27;</span>, <span class="string">&#x27;age&#x27;</span>: <span class="number">18</span>, <span class="string">&#x27;gender&#x27;</span>: <span class="string">&#x27;男&#x27;</span>&#125;</span><br><span class="line">person[<span class="string">&#x27;name&#x27;</span>] = <span class="string">&#x27;法外狂徒张三&#x27;</span></span><br><span class="line"><span class="built_in">print</span>(person.get(<span class="string">&#x27;name&#x27;</span>))  <span class="comment"># 输出：法外狂徒张三</span></span><br></pre></td></tr></table></figure>
<h4 id="7-5-3、字典添加"><a href="#7-5-3、字典添加" class="headerlink" title="7.5.3、字典添加"></a>7.5.3、字典添加</h4><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">person = &#123;<span class="string">&#x27;name&#x27;</span>: <span class="string">&#x27;韩信&#x27;</span>, <span class="string">&#x27;age&#x27;</span>: <span class="number">18</span>&#125;</span><br><span class="line">person[<span class="string">&#x27;gender&#x27;</span>] = <span class="string">&#x27;男&#x27;</span></span><br><span class="line"><span class="built_in">print</span>(person.get(<span class="string">&#x27;gender&#x27;</span>))  <span class="comment"># 输出：男</span></span><br></pre></td></tr></table></figure>
<h4 id="7-5-4、字典删除"><a href="#7-5-4、字典删除" class="headerlink" title="7.5.4、字典删除"></a>7.5.4、字典删除</h4><ul>
<li>del ：删除指定的元素 / 删除整个字典</li>
<li>clear()：清空整个字典</li>
</ul>
<p><strong>del和clear删除字典的区别为：del删除整个字典，如果删除之后还调用，会报未定义错误。clear则是将字典中的元素全部删除，但是字典还是存在的</strong></p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line">person = &#123;<span class="string">&#x27;name&#x27;</span>: <span class="string">&#x27;韩信&#x27;</span>, <span class="string">&#x27;age&#x27;</span>: <span class="number">18</span>&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">del</span> person[<span class="string">&#x27;age&#x27;</span>]  <span class="comment"># 删除指定key的元素</span></span><br><span class="line"><span class="built_in">print</span>(person)  <span class="comment"># 输出：&#123;&#x27;name&#x27;: &#x27;韩信&#x27;&#125;</span></span><br><span class="line"></span><br><span class="line">person.clear()  <span class="comment"># 清除字典中的所有键值对</span></span><br><span class="line"><span class="built_in">print</span>(person)  <span class="comment"># 输出：&#123;&#125;</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">del</span> person  <span class="comment"># 删除整个字典，把整个变量都删除</span></span><br><span class="line"><span class="built_in">print</span>(person)  <span class="comment"># 报错：NameError: name &#x27;person&#x27; is not defined</span></span><br></pre></td></tr></table></figure>
<h4 id="7-5-5、字典遍历"><a href="#7-5-5、字典遍历" class="headerlink" title="7.5.5、字典遍历"></a>7.5.5、字典遍历</h4><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br></pre></td><td class="code"><pre><span class="line">person = &#123;<span class="string">&#x27;name&#x27;</span>: <span class="string">&#x27;韩信&#x27;</span>, <span class="string">&#x27;age&#x27;</span>: <span class="number">18</span>&#125;</span><br><span class="line"></span><br><span class="line"><span class="string">&quot;&quot;&quot;</span></span><br><span class="line"><span class="string">遍历 key</span></span><br><span class="line"><span class="string">输出：</span></span><br><span class="line"><span class="string">    name</span></span><br><span class="line"><span class="string">    age</span></span><br><span class="line"><span class="string">&quot;&quot;&quot;</span></span><br><span class="line"><span class="keyword">for</span> item <span class="keyword">in</span> person.keys():</span><br><span class="line">    <span class="built_in">print</span>(item)</span><br><span class="line"></span><br><span class="line"><span class="string">&quot;&quot;&quot;</span></span><br><span class="line"><span class="string">遍历 value</span></span><br><span class="line"><span class="string">输出：</span></span><br><span class="line"><span class="string">    韩信</span></span><br><span class="line"><span class="string">    18</span></span><br><span class="line"><span class="string">&quot;&quot;&quot;</span></span><br><span class="line"><span class="keyword">for</span> item <span class="keyword">in</span> person.values():</span><br><span class="line">    <span class="built_in">print</span>(item)</span><br><span class="line"></span><br><span class="line"><span class="string">&quot;&quot;&quot;</span></span><br><span class="line"><span class="string">遍历键值对：（key,value)</span></span><br><span class="line"><span class="string">输出：</span></span><br><span class="line"><span class="string">    (&#x27;name&#x27;, &#x27;韩信&#x27;)</span></span><br><span class="line"><span class="string">    (&#x27;age&#x27;, 18)</span></span><br><span class="line"><span class="string">&quot;&quot;&quot;</span></span><br><span class="line"><span class="keyword">for</span> item <span class="keyword">in</span> person.items():</span><br><span class="line">    <span class="built_in">print</span>(item)</span><br></pre></td></tr></table></figure>
<h2 id="8、函数"><a href="#8、函数" class="headerlink" title="8、函数"></a>8、函数</h2><h3 id="8-1、函数定义"><a href="#8-1、函数定义" class="headerlink" title="8.1、函数定义"></a>8.1、函数定义</h3><blockquote>
<p><strong>格式</strong></p>
</blockquote>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">def</span> <span class="title function_">函数名</span>():</span><br><span class="line">	代码</span><br></pre></td></tr></table></figure>
<blockquote>
<p><strong>示例</strong></p>
</blockquote>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">def</span> <span class="title function_">personInfo</span>():</span><br><span class="line">    <span class="built_in">print</span>(<span class="string">&#x27;name：&#x27;</span> + <span class="string">&#x27;法外狂徒张三&#x27;</span>)</span><br><span class="line">    <span class="built_in">print</span>(<span class="string">&#x27;age：&#x27;</span> + <span class="string">&#x27;18&#x27;</span>)</span><br></pre></td></tr></table></figure>
<h3 id="8-2、函数调用"><a href="#8-2、函数调用" class="headerlink" title="8.2、函数调用"></a>8.2、函数调用</h3><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">def</span> <span class="title function_">personInfo</span>():</span><br><span class="line">    <span class="built_in">print</span>(<span class="string">&#x27;name：&#x27;</span> + <span class="string">&#x27;法外狂徒张三&#x27;</span>)</span><br><span class="line">    <span class="built_in">print</span>(<span class="string">&#x27;age：&#x27;</span> + <span class="string">&#x27;18&#x27;</span>)</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">personInfo()</span><br></pre></td></tr></table></figure>
<h3 id="8-3、函数参数"><a href="#8-3、函数参数" class="headerlink" title="8.3、函数参数"></a>8.3、函数参数</h3><p><strong>函数的参数可以给默认值，当没有值传入的时候，会自动使用默认值,，如果没有默认值，则在调用的时候必须传值，也可以在调用函数的时候给指定的参数传值</strong></p>
<blockquote>
<p><strong>固定参数</strong></p>
</blockquote>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">def</span> <span class="title function_">personInfo</span>(<span class="params">gender, name=<span class="string">&#x27;李白&#x27;</span>, age=<span class="number">20</span></span>):</span><br><span class="line">    <span class="built_in">print</span>(<span class="string">&#x27;gender：%s&#x27;</span> % gender)  <span class="comment"># 输出：gender：男</span></span><br><span class="line">    <span class="built_in">print</span>(<span class="string">&#x27;name：%s&#x27;</span> % name)  <span class="comment"># 输出：name：韩信</span></span><br><span class="line">    <span class="built_in">print</span>(<span class="string">&#x27;age：%d&#x27;</span> % age)  <span class="comment"># 输出：age：22</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment">#位置参数传参</span></span><br><span class="line">personInfo(<span class="string">&#x27;男&#x27;</span>, <span class="string">&#x27;韩信&#x27;</span>)  <span class="comment"># 不传入对应的值，如果有默认值，则不报错，否则报错</span></span><br><span class="line"></span><br><span class="line"><span class="comment">#关键字传参</span></span><br><span class="line">personInfo(gender=<span class="string">&#x27;女&#x27;</span>, name=<span class="string">&#x27;杨玉环&#x27;</span>)  <span class="comment"># 给指定的变量赋值，如果有一个使用了指定传值，其他都要指定</span></span><br></pre></td></tr></table></figure>
<blockquote>
<p><strong>可变参数</strong></p>
</blockquote>
<p><strong>python中会将最后没有变量接收的参数会使用元组统一接收</strong></p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">def</span> <span class="title function_">personInfo</span>(<span class="params">name, *aaa</span>):</span><br><span class="line">    <span class="built_in">print</span>(name)  <span class="comment"># 输出：韩信</span></span><br><span class="line">    <span class="built_in">print</span>(aaa)  <span class="comment"># 输出：(1, 2, 3, 45, 5)</span></span><br><span class="line">    <span class="built_in">print</span>(<span class="built_in">type</span>(aaa))  <span class="comment"># 输出：&lt;class &#x27;tuple&#x27;&gt;</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line">personInfo(<span class="string">&#x27;韩信&#x27;</span>, <span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">45</span>, <span class="number">5</span>)</span><br></pre></td></tr></table></figure>
<h3 id="8-4、函数返回值"><a href="#8-4、函数返回值" class="headerlink" title="8.4、函数返回值"></a>8.4、函数返回值</h3><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">def</span> <span class="title function_">personInfo</span>(<span class="params">age</span>):</span><br><span class="line">    <span class="keyword">return</span> age + <span class="number">1</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="built_in">print</span>(personInfo(<span class="number">1</span>))  <span class="comment"># 输出：2</span></span><br></pre></td></tr></table></figure>
<h3 id="8-5、局部变量"><a href="#8-5、局部变量" class="headerlink" title="8.5、局部变量"></a>8.5、局部变量</h3><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">def</span> <span class="title function_">personInfo</span>(<span class="params">age</span>):</span><br><span class="line">    b = <span class="number">2</span>  <span class="comment"># 局部变量</span></span><br><span class="line">    <span class="keyword">return</span> age + b</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="built_in">print</span>(personInfo(<span class="number">1</span>))  <span class="comment"># 输出：3</span></span><br></pre></td></tr></table></figure>
<h3 id="8-6、全局变量"><a href="#8-6、全局变量" class="headerlink" title="8.6、全局变量"></a>8.6、全局变量</h3><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">a = <span class="number">1</span>  <span class="comment"># 全局变量</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="keyword">def</span> <span class="title function_">personInfo</span>(<span class="params">age</span>):</span><br><span class="line">    b = <span class="number">2</span>  <span class="comment"># 局部变量</span></span><br><span class="line">    <span class="keyword">return</span> age + b + a</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="built_in">print</span>(personInfo(<span class="number">1</span>))  <span class="comment"># 输出：4</span></span><br></pre></td></tr></table></figure>
<h2 id="10、文件"><a href="#10、文件" class="headerlink" title="10、文件"></a>10、文件</h2><h3 id="10-1、文件的打开与关闭"><a href="#10-1、文件的打开与关闭" class="headerlink" title="10.1、文件的打开与关闭"></a>10.1、文件的打开与关闭</h3><h4 id="10-1-1、打开文件-创建文件"><a href="#10-1-1、打开文件-创建文件" class="headerlink" title="10.1.1、打开文件/创建文件"></a>10.1.1、打开文件/创建文件</h4><blockquote>
<p><strong>格式</strong></p>
</blockquote>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 当文件存在时是打开，当文件不存在时，创建文件</span></span><br><span class="line"><span class="built_in">open</span>(文件路径,模式)</span><br><span class="line"><span class="string">&#x27;&#x27;&#x27;</span></span><br><span class="line"><span class="string">模式：</span></span><br><span class="line"><span class="string">	w：可写</span></span><br><span class="line"><span class="string">	r：可读</span></span><br><span class="line"><span class="string">&#x27;&#x27;&#x27;</span></span><br></pre></td></tr></table></figure>
<blockquote>
<p><strong>示例</strong></p>
</blockquote>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 指定文件，设置操作模式，设置字符编码</span></span><br><span class="line"><span class="built_in">open</span>(file=<span class="string">&#x27;test1.txt&#x27;</span>, mode=<span class="string">&#x27;r&#x27;</span>, encoding=<span class="string">&#x27;utf-8&#x27;</span>)</span><br></pre></td></tr></table></figure>
<blockquote>
<p><strong>文件路径</strong></p>
</blockquote>
<ul>
<li>绝对路径：指的是绝对位置，完整地描述了目标的所在地，所有目录层级关系是一目了然的。 、<ul>
<li>例如：<code>E:\python</code>，从电脑的盘符开始，表示的就是一个绝对路径。</li>
</ul>
</li>
<li>相对路径：是从当前文件所在的文件夹开始的路径。<ul>
<li><code>test.txt</code> ，是在当前文件夹查找 test.txt 文件 </li>
<li><code>./test.txt</code> ，也是在当前文件夹里查找 test.txt 文件， <code>./</code>表示的是当前文件夹。 </li>
<li><code>../test.txt</code> ，从当前文件夹的上一级文件夹里查找 <code>test.txt</code> 文件。 <code>../</code>表示的是上一级文件夹 </li>
<li><code>demo/test.txt</code> ，在当前文件夹里查找 <code>demo</code> 这个文件夹，并在这个文件夹里查找 <code>test.txt</code> 文件。</li>
</ul>
</li>
</ul>
<blockquote>
<p><strong>访问模式</strong></p>
</blockquote>
<div class="table-container">
<table>
<thead>
<tr>
<th>访问模式</th>
<th>说明</th>
</tr>
</thead>
<tbody>
<tr>
<td>r</td>
<td>以只读方式打开文件。文件的指针将会放在文件的开头。如果文件不存在，则报错。<strong>这是默认模式</strong></td>
</tr>
<tr>
<td>w</td>
<td>打开一个文件只用于写入。如果该文件已存在则将其覆盖。如果该文件不存在，创建新文件。</td>
</tr>
<tr>
<td>a</td>
<td>打开一个文件用于追加。如果该文件已存在，文件指针将会放在文件的结尾。也就是说，新的内容将 会被写入到已有内容之后。如果该文件不存在，创建新文件进行写入。</td>
</tr>
<tr>
<td>r+</td>
<td>打开一个文件用于读写。文件指针将会放在文件的开头。</td>
</tr>
<tr>
<td>w+</td>
<td>打开一个文件用于读写。如果该文件已存在则将其覆盖。如果该文件不存在，创建新文件。</td>
</tr>
<tr>
<td>a+</td>
<td>打开一个文件用于读写。如果该文件已存在，文件指针将会放在文件的结尾。文件打开时会是追加模 式。如果该文件不存在，创建新文件用于读写。</td>
</tr>
<tr>
<td>rb</td>
<td>以二进制格式打开一个文件用于只读。文件指针将会放在文件的开头</td>
</tr>
<tr>
<td>wb</td>
<td>以二进制格式打开一个文件只用于写入。如果该文件已存在则将其覆盖。如果该文件不存在，创建新 文件</td>
</tr>
<tr>
<td>ab</td>
<td>以二进制格式打开一个文件用于追加。如果该文件已存在，文件指针将会放在文件的结尾。也就是 说，新的内容将会被写入到已有内容之后。如果该文件不存在，创建新文件进行写入。</td>
</tr>
<tr>
<td>rb+</td>
<td>以二进制格式打开一个文件用于读写。文件指针将会放在文件的开头。</td>
</tr>
<tr>
<td>wb+</td>
<td>以二进制格式打开一个文件用于读写。如果该文件已存在则将其覆盖。如果该文件不存在，创建新文 件。</td>
</tr>
<tr>
<td>ab+</td>
<td>以二进制格式打开一个文件用于读写。如果该文件已存在，文件指针将会放在文件的结尾。如果该文 件不存在，创建新文件用于读写。</td>
</tr>
</tbody>
</table>
</div>
<h3 id="10-2、文件的读写"><a href="#10-2、文件的读写" class="headerlink" title="10.2、文件的读写"></a>10.2、文件的读写</h3><blockquote>
<p><strong>写数据(write)</strong></p>
</blockquote>
<p><strong>如果文件不存在，那么创建；如果存在那么就先清空，然后写入数据</strong></p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 指定文件，设置操作模式，设置字符编码</span></span><br><span class="line">f = <span class="built_in">open</span>(file=<span class="string">&#x27;test1.txt&#x27;</span>, mode=<span class="string">&#x27;w&#x27;</span>, encoding=<span class="string">&#x27;utf-8&#x27;</span>)</span><br><span class="line">f.write(<span class="string">&quot;乱码了吗&quot;</span>)</span><br><span class="line">f.close()</span><br></pre></td></tr></table></figure>
<blockquote>
<p><strong>读数据(read)</strong></p>
</blockquote>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">f = <span class="built_in">open</span>(file=<span class="string">&#x27;test1.txt&#x27;</span>, mode=<span class="string">&#x27;r&#x27;</span>, encoding=<span class="string">&#x27;utf-8&#x27;</span>)</span><br><span class="line">result = f.read(<span class="number">5</span>)  <span class="comment"># 最多读取5个数据</span></span><br><span class="line"><span class="built_in">print</span>(result)</span><br><span class="line">f.close()</span><br></pre></td></tr></table></figure>
<blockquote>
<p><strong>读数据（readlines）</strong></p>
</blockquote>
<p><strong>readlines可以按照行的方式把整个文件中的内容进行一次性读取，并且返回的是一个列表，其中每一行为列表的 一个元素。换行也会读取</strong></p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 指定文件，设置操作模式，设置字符编码</span></span><br><span class="line">f = <span class="built_in">open</span>(file=<span class="string">&#x27;test1.txt&#x27;</span>, mode=<span class="string">&#x27;r&#x27;</span>, encoding=<span class="string">&#x27;utf-8&#x27;</span>)</span><br><span class="line">result = f.readlines()  <span class="comment"># 最多读取5个数据</span></span><br><span class="line"><span class="built_in">print</span>(result)</span><br><span class="line">f.close()</span><br></pre></td></tr></table></figure>
<h3 id="10-3、序列化和反序列化"><a href="#10-3、序列化和反序列化" class="headerlink" title="10.3、序列化和反序列化"></a>10.3、序列化和反序列化</h3><p>通过文件操作，我们可以将字符串写入到一个本地文件。但是，如果是一个对象(例如列表、字典、元组等)，就无 法直接写入到一个文件里，需要对这个对象进行序列化，然后才能写入到文件里。 </p>
<p>设计一套协议，按照某种规则，把内存中的数据转换为字节序列，保存到文件，这就是序列化，反之，从文件的字 节序列恢复到内存中，就是反序列化</p>
<p>对象—-》字节序列 === 序列化</p>
<p>字节序列—》对象 ===反序列化</p>
<p><strong>Python中提供了JSON这个模块用来实现数据的序列化和反序列化</strong></p>
<blockquote>
<p><strong>JSON模块</strong></p>
</blockquote>
<p>JSON(JavaScriptObjectNotation, JS对象简谱)是一种轻量级的数据交换标准。JSON的本质是字符串。</p>
<blockquote>
<p><strong>使用JSON实现序列化</strong></p>
</blockquote>
<p><strong>JSON提供了dump和dumps方法，将一个对象进行序列化。</strong></p>
<p><strong><code>dumps</code>方法的作用是把对象转换成为字符串，它本身不具备将数据写入到文件的功能。</strong></p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> json</span><br><span class="line">file = <span class="built_in">open</span>(<span class="string">&#x27;names.txt&#x27;</span>, <span class="string">&#x27;w&#x27;</span>)</span><br><span class="line">names = [<span class="string">&#x27;zhangsan&#x27;</span>, <span class="string">&#x27;lisi&#x27;</span>, <span class="string">&#x27;wangwu&#x27;</span>, <span class="string">&#x27;jerry&#x27;</span>, <span class="string">&#x27;henry&#x27;</span>, <span class="string">&#x27;merry&#x27;</span>, <span class="string">&#x27;chris&#x27;</span>]</span><br><span class="line"><span class="comment"># file.write(names) 出错，不能直接将列表写入到文件里</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># 可以调用 json的dumps方法，传入一个对象参数</span></span><br><span class="line">result = json.dumps(names)</span><br><span class="line"></span><br><span class="line"><span class="comment"># dumps 方法得到的结果是一个字符串</span></span><br><span class="line"><span class="built_in">print</span>(<span class="built_in">type</span>(result)) <span class="comment"># &lt;class &#x27;str&#x27;&gt;</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># 可以将字符串写入到文件里</span></span><br><span class="line">file.write(result)</span><br><span class="line">file.close()</span><br></pre></td></tr></table></figure>
<p><strong><code>dump</code>方法可以在将对象转换成为字符串的同时，指定一个文件对象，把转换后的字符串写入到这个文件里</strong></p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> json</span><br><span class="line"></span><br><span class="line">file = <span class="built_in">open</span>(<span class="string">&#x27;names.txt&#x27;</span>, <span class="string">&#x27;w&#x27;</span>)</span><br><span class="line">names = [<span class="string">&#x27;zhangsan&#x27;</span>, <span class="string">&#x27;lisi&#x27;</span>, <span class="string">&#x27;wangwu&#x27;</span>, <span class="string">&#x27;jerry&#x27;</span>, <span class="string">&#x27;henry&#x27;</span>, <span class="string">&#x27;merry&#x27;</span>, <span class="string">&#x27;chris&#x27;</span>]</span><br><span class="line"></span><br><span class="line"><span class="comment"># dump方法可以接收一个文件参数，在将对象转换成为字符串的同时写入到文件里</span></span><br><span class="line">json.dump(names, file)</span><br><span class="line">file.close()</span><br></pre></td></tr></table></figure>
<blockquote>
<p><strong>使用JSON实现反序列化</strong></p>
</blockquote>
<p><strong>使用loads和load方法，可以将一个JSON字符串反序列化成为一个Python对象。</strong></p>
<p><strong><code>loads</code>方法需要一个字符串参数，用来将一个字符串加载成为Python对象。</strong></p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> json</span><br><span class="line"></span><br><span class="line"><span class="comment"># 调用loads方法，传入一个字符串，可以将这个字符串加载成为Python对象</span></span><br><span class="line">result = json.loads(<span class="string">&#x27;[&quot;zhangsan&quot;, &quot;lisi&quot;, &quot;wangwu&quot;, &quot;jerry&quot;, &quot;henry&quot;, &quot;merry&quot;, &quot;chris&quot;]&#x27;</span>)</span><br><span class="line"><span class="built_in">print</span>(<span class="built_in">type</span>(result)) <span class="comment"># &lt;class &#x27;list&#x27;&gt;</span></span><br></pre></td></tr></table></figure>
<p><strong>load方法可以传入一个文件对象，用来将一个文件对象里的数据加载成为Python对象。</strong></p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> json</span><br><span class="line"></span><br><span class="line"><span class="comment"># 以可读方式打开一个文件</span></span><br><span class="line">file = <span class="built_in">open</span>(<span class="string">&#x27;names.txt&#x27;</span>, <span class="string">&#x27;r&#x27;</span>)</span><br><span class="line"></span><br><span class="line"><span class="comment"># 调用load方法，将文件里的内容加载成为一个Python对象</span></span><br><span class="line">result = json.load(file)</span><br><span class="line"><span class="built_in">print</span>(result)</span><br><span class="line">file.close()</span><br></pre></td></tr></table></figure>
<h2 id="11、异常"><a href="#11、异常" class="headerlink" title="11、异常"></a>11、异常</h2><blockquote>
<p><strong>格式</strong></p>
</blockquote>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">try</span>:</span><br><span class="line">    可能会出现异常的代码块</span><br><span class="line"><span class="keyword">except</span> 异常的类型:</span><br><span class="line">	出现异常以后的处理语句</span><br></pre></td></tr></table></figure>
<blockquote>
<p><strong>示例</strong></p>
</blockquote>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">try</span>:</span><br><span class="line">    a = <span class="number">10</span> / <span class="number">0</span></span><br><span class="line"><span class="keyword">except</span> ZeroDivisionError:</span><br><span class="line">    <span class="built_in">print</span>(<span class="string">&quot;程序出异常了&quot;</span>)</span><br></pre></td></tr></table></figure>
<h1 id="二、爬虫技术"><a href="#二、爬虫技术" class="headerlink" title="二、爬虫技术"></a>二、爬虫技术</h1><h2 id="1、urllib"><a href="#1、urllib" class="headerlink" title="1、urllib"></a>1、urllib</h2><h3 id="1-1、urllib库使用"><a href="#1-1、urllib库使用" class="headerlink" title="1.1、urllib库使用"></a>1.1、urllib库使用</h3><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line">urllib.request.urlopen() 模拟浏览器向服务器发送请求</span><br><span class="line">response 服务器返回的数据</span><br><span class="line">	response的数据类型是HttpResponse</span><br><span class="line">    字节‐‐&gt;字符串</span><br><span class="line">  	 	 解码decode</span><br><span class="line">    字符串‐‐&gt;字节</span><br><span class="line">  	 	 编码encode</span><br><span class="line">    read() 字节形式读取二进制 扩展：rede(<span class="number">5</span>)返回前几个字节</span><br><span class="line">    readline() 读取一行</span><br><span class="line">    readlines() 一行一行读取 直至结束</span><br><span class="line">    getcode() 获取状态码</span><br><span class="line">    geturl() 获取url</span><br><span class="line">    getheaders() 获取headers</span><br><span class="line">urllib.request.urlretrieve()</span><br><span class="line">    请求网页</span><br><span class="line">    请求图片</span><br><span class="line">    请求视频</span><br><span class="line"></span><br></pre></td></tr></table></figure>
<blockquote>
<p><strong>一个类型和六个方法</strong></p>
</blockquote>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 引入包</span></span><br><span class="line"><span class="keyword">import</span> urllib.request</span><br><span class="line"></span><br><span class="line"><span class="comment"># 1、定义一个url 就是需要访问的地址</span></span><br><span class="line">url = <span class="string">&quot;http://www.baidu.com&quot;</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># 2、模拟浏览器向服务器发送请求</span></span><br><span class="line">response = urllib.request.urlopen(url)</span><br><span class="line"></span><br><span class="line"><span class="comment"># 3、一个类型和六个方法</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># print(type(response))  # 输出：&lt;class &#x27;http.client.HTTPResponse&#x27;&gt;</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># 按照一个一个字节去读，读取全部字节</span></span><br><span class="line"><span class="comment"># content = response.read()</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># 按照一个一个字节去读，读取字节个数为自定个数</span></span><br><span class="line"><span class="comment"># content = response.read(5)</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># 读取一行字节</span></span><br><span class="line"><span class="comment"># content = response.readline()</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># 读取全部</span></span><br><span class="line"><span class="comment"># content = response.readlines()</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># 获取返回的状态码</span></span><br><span class="line"><span class="comment"># statusCode = response.getcode()</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># 返回请求的地址</span></span><br><span class="line"><span class="comment"># reqUrl = response.geturl()</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># 获取状态信息</span></span><br><span class="line"><span class="comment"># print(response.getheaders())</span></span><br></pre></td></tr></table></figure>
<blockquote>
<p><strong>网页、图片、视频下载</strong></p>
</blockquote>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> urllib.request</span><br><span class="line"></span><br><span class="line"><span class="comment"># 下载网页</span></span><br><span class="line"><span class="comment"># urlPage = &#x27;http://www.baidu.com&#x27;</span></span><br><span class="line"><span class="comment"># urllib.request.urlretrieve(urlPage, &#x27;百度首页.html&#x27;)</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># 下载图片</span></span><br><span class="line"><span class="comment"># urlImage = &#x27;图片地址&#x27;</span></span><br><span class="line"><span class="comment"># urllib.request.urlretrieve(url=urlImage, filename=&#x27;图片.webp&#x27;)</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># 下载视频</span></span><br><span class="line"><span class="comment"># urlVideo = &#x27;视频地址&#x27;</span></span><br><span class="line"><span class="comment"># urllib.request.urlretrieve(url=urlVideo, filename=&#x27;视频.mp4&#x27;)</span></span><br></pre></td></tr></table></figure>
<h3 id="1-2、请求对象定制"><a href="#1-2、请求对象定制" class="headerlink" title="1.2、请求对象定制"></a>1.2、请求对象定制</h3><p><strong>UA介绍：User Agent中文名为用户代理，简称 UA，它是一个特殊字符串头，使得服务器能够识别客户使用的操作系统 及版本、CPU 类型、浏览器及版本。浏览器内核、浏览器渲染引擎、浏览器语言、浏览器插件等</strong></p>
<blockquote>
<p><strong>URL组成</strong></p>
</blockquote>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># https://www.baidu.com/s?wd=李白</span></span><br><span class="line"><span class="comment"># http/https   www.baidu.com    80/443     	 s         wd=李白            #</span></span><br><span class="line">	 协议			   主机			端口号		路径		  参数			 锚点		</span><br></pre></td></tr></table></figure>
<blockquote>
<p><strong>示例</strong></p>
</blockquote>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> urllib.request</span><br><span class="line"><span class="comment"># 访问地址</span></span><br><span class="line">url = <span class="string">&#x27;https://www.baidu.com&#x27;</span></span><br><span class="line"><span class="comment"># 定制请求头</span></span><br><span class="line">headers = &#123;</span><br><span class="line">    <span class="string">&#x27;User-Agent&#x27;</span>: <span class="string">&#x27;Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/93.0.4577.63 Safari/537.36&#x27;</span></span><br><span class="line">&#125;</span><br><span class="line"><span class="comment"># 设置请求对象</span></span><br><span class="line">request = urllib.request.Request(url=url, headers=headers)</span><br><span class="line"><span class="comment"># 访问地址</span></span><br><span class="line">response = urllib.request.urlopen(request)</span><br><span class="line"></span><br><span class="line">content = response.read().decode(<span class="string">&#x27;utf-8&#x27;</span>)</span><br><span class="line"></span><br><span class="line"><span class="built_in">print</span>(content)</span><br></pre></td></tr></table></figure>
<h3 id="1-3、编解码"><a href="#1-3、编解码" class="headerlink" title="1.3、编解码"></a>1.3、编解码</h3><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="string">&#x27;&#x27;&#x27;编码集的演变‐‐‐</span></span><br><span class="line"><span class="string">由于计算机是美国人发明的，因此，最早只有127个字符被编码到计算机里，也就是大小写英文字母、数字和一些符号，</span></span><br><span class="line"><span class="string">这个编码表被称为ASCII编码，比如大写字母A的编码是65，小写字母z的编码是122。</span></span><br><span class="line"><span class="string">但是要处理中文显然一个字节是不够的，至少需要两个字节，而且还不能和ASCII编码冲突，</span></span><br><span class="line"><span class="string">所以，中国制定了GB2312编码，用来把中文编进去。</span></span><br><span class="line"><span class="string">你可以想得到的是，全世界有上百种语言，日本把日文编到Shift_JIS里，韩国把韩文编到Euc‐kr里，</span></span><br><span class="line"><span class="string">各国有各国的标准，就会不可避免地出现冲突，结果就是，在多语言混合的文本中，显示出来会有乱码。</span></span><br><span class="line"><span class="string">因此，Unicode应运而生。Unicode把所有语言都统一到一套编码里，这样就不会再有乱码问题了。</span></span><br><span class="line"><span class="string">Unicode标准也在不断发展，但最常用的是用两个字节表示一个字符（如果要用到非常偏僻的字符，就需要4个字节）。</span></span><br><span class="line"><span class="string">现代操作系统和大多数编程语言都直接支持Unicode。&#x27;&#x27;&#x27;</span></span><br></pre></td></tr></table></figure>
<h4 id="1-3-1、Get请求参数编码"><a href="#1-3-1、Get请求参数编码" class="headerlink" title="1.3.1、Get请求参数编码"></a>1.3.1、Get请求参数编码</h4><p><strong>使用<code>quoto、urlencode</code>将参数设置为Unicode编码，否则会报错</strong></p>
<p><strong><code>quoto、urlencode</code>的区别为：quoto需要一个一个拼接，urlencode可以将一个字典中的参数一次性拼接</strong></p>
<blockquote>
<p><strong>get请求方式：urllib.parse.quote（）</strong></p>
</blockquote>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> urllib.request</span><br><span class="line"><span class="keyword">import</span> urllib.parse</span><br><span class="line"></span><br><span class="line">url = <span class="string">&#x27;https://www.baidu.com/s?wd=&#x27;</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># 将url的请求参数设置为Unicode编码</span></span><br><span class="line">url = url + urllib.parse.quote(<span class="string">&#x27;周杰伦&#x27;</span>)</span><br><span class="line"></span><br><span class="line"><span class="comment"># 请求对象订单</span></span><br><span class="line">headers = &#123;</span><br><span class="line">    <span class="string">&#x27;User-Agent&#x27;</span>: <span class="string">&#x27;Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/93.0.4577.63 Safari/537.36&#x27;</span></span><br><span class="line">&#125;</span><br><span class="line">request = urllib.request.Request(url=url, headers=headers)</span><br><span class="line"></span><br><span class="line"><span class="comment"># 模拟浏览器发送请求</span></span><br><span class="line">response = urllib.request.urlopen(request)</span><br><span class="line"></span><br><span class="line"><span class="comment"># 获取响应的数据</span></span><br><span class="line">content = response.read().decode(<span class="string">&#x27;utf-8&#x27;</span>)</span><br><span class="line"></span><br><span class="line"><span class="built_in">print</span>(content)</span><br></pre></td></tr></table></figure>
<blockquote>
<p><strong>get请求方式：urllib.parse.urlencode（）</strong></p>
</blockquote>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> urllib.request</span><br><span class="line"><span class="keyword">import</span> urllib.parse</span><br><span class="line"></span><br><span class="line">url = <span class="string">&#x27;https://www.baidu.com/s?&#x27;</span></span><br><span class="line"></span><br><span class="line">data = &#123;</span><br><span class="line">    <span class="string">&#x27;wd&#x27;</span>: <span class="string">&#x27;周杰伦&#x27;</span>,</span><br><span class="line">    <span class="string">&#x27;aaa&#x27;</span>: <span class="literal">None</span></span><br><span class="line">&#125;</span><br><span class="line">data = urllib.parse.urlencode(data)</span><br><span class="line"></span><br><span class="line"><span class="comment"># 将url的请求参数设置为Unicode编码</span></span><br><span class="line">url = url + data</span><br><span class="line"></span><br><span class="line"><span class="comment"># 请求对象订单</span></span><br><span class="line">headers = &#123;</span><br><span class="line">    <span class="string">&#x27;User-Agent&#x27;</span>: <span class="string">&#x27;Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/93.0.4577.63 Safari/537.36&#x27;</span></span><br><span class="line">&#125;</span><br><span class="line">request = urllib.request.Request(url=url, headers=headers)</span><br><span class="line"></span><br><span class="line"><span class="comment"># 模拟浏览器发送请求</span></span><br><span class="line">response = urllib.request.urlopen(request)</span><br><span class="line"></span><br><span class="line"><span class="comment"># 获取响应的数据</span></span><br><span class="line">content = response.read().decode(<span class="string">&#x27;utf-8&#x27;</span>)</span><br><span class="line"></span><br><span class="line"><span class="built_in">print</span>(content)</span><br></pre></td></tr></table></figure>
<h4 id="1-3-2、Post请求"><a href="#1-3-2、Post请求" class="headerlink" title="1.3.2、Post请求"></a>1.3.2、Post请求</h4><blockquote>
<p><strong>post请求方式</strong></p>
</blockquote>
<ul>
<li><p><strong>百度翻译简单翻译</strong></p>
  <figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> urllib.request</span><br><span class="line"><span class="keyword">import</span> urllib.parse</span><br><span class="line"><span class="keyword">import</span> json</span><br><span class="line"></span><br><span class="line">url = <span class="string">&#x27;https://fanyi.baidu.com/sug&#x27;</span></span><br><span class="line"></span><br><span class="line">headers = &#123;</span><br><span class="line">    <span class="string">&#x27;User-Agent&#x27;</span>: <span class="string">&#x27;Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/93.0.4577.63 Safari/537.36&#x27;</span></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">keyword = <span class="built_in">input</span>(<span class="string">&#x27;输入要查询的单词：&#x27;</span>)</span><br><span class="line">data = &#123;</span><br><span class="line">    <span class="string">&#x27;kw&#x27;</span>: keyword</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment"># post请求还需要再调用一次encode()方法</span></span><br><span class="line">data = urllib.parse.urlencode(data).encode(<span class="string">&#x27;utf-8&#x27;</span>)</span><br><span class="line"></span><br><span class="line"><span class="comment"># post的请求参数，是不会拼接在url后面的，二十需要放在请求对象定制的参数中</span></span><br><span class="line">request = urllib.request.Request(url=url, data=data, headers=headers)</span><br><span class="line"></span><br><span class="line"><span class="comment"># 模拟浏览器发送请求</span></span><br><span class="line">response = urllib.request.urlopen(request)</span><br><span class="line"></span><br><span class="line"><span class="comment"># 获取响应的数据</span></span><br><span class="line">content = response.read().decode(<span class="string">&#x27;utf-8&#x27;</span>)</span><br><span class="line"></span><br><span class="line"><span class="comment"># 将返回的字符串转换为JSON对象</span></span><br><span class="line"><span class="built_in">print</span>(json.loads(content))</span><br></pre></td></tr></table></figure>
</li>
<li><p><strong>百度翻译详细翻译</strong></p>
  <figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> urllib.request</span><br><span class="line"><span class="keyword">import</span> urllib.parse</span><br><span class="line"><span class="keyword">import</span> json</span><br><span class="line"></span><br><span class="line">url = <span class="string">&#x27;https://fanyi.baidu.com/v2transapi?from=en&amp;to=zh&#x27;</span></span><br><span class="line"></span><br><span class="line">headers = &#123;</span><br><span class="line">    <span class="string">&#x27;User-Agent&#x27;</span>: <span class="string">&#x27;Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/93.0.4577.63 Safari/537.36&#x27;</span>,</span><br><span class="line">    <span class="string">&#x27;Cookie&#x27;</span>: <span class="string">&#x27;BIDUPSID=112CB6BA1DD43128CF60D2C21C3891FA; PSTM=1630486205; BAIDUID=112CB6BA1DD43128A6786F0F3F6C98E3:FG=1; BDORZ=B490B5EBF6F3CD402E515D22BCDA1598; __yjs_duid=1_e631d45aca5951ce99da01dbce8657761630487228976; REALTIME_TRANS_SWITCH=1; FANYI_WORD_SWITCH=1; HISTORY_SWITCH=1; SOUND_PREFER_SWITCH=1; SOUND_SPD_SWITCH=1; Hm_lvt_64ecd82404c51e03dc91cb9e8c025574=1630487229,1630489993,1630644060; delPer=0; PSINO=7; H_PS_PSSID=34434_34496_31660_33848_34524_34092_34107_26350_34426_34319_34473; BA_HECTOR=8k21a424248ka504cq1gj3atu0r; Hm_lpvt_64ecd82404c51e03dc91cb9e8c025574=1630647380; ab_sr=1.0.1_MDRkZDYwZjI4MzQzYjYyNTU5YTYwYTBmNWNkNTYwY2EwZTM1MWVmZDc5NTIwMmZhN2UxMWFmNDI5NzZmZWFjMDBkNDRiZTE2MTIxNjdhZDBhYWU2NDE1NGQ2M2FlN2ZhNThmYTQ2ODIyOGMzMDY0MWE2ZWQ2MjFmZjBlZWJkMmRjMzE3ZDhmMDYwNTFhODEyNzA5YWFjY2FiNjU4OGY2ZA==; __yjs_st=2_YmE3ZWYzYjZhODc4NTYxOWU4ZWQ2OTg2NTdmMzVjYmJkYjlkMmQ3YmZhYzliYWExMTI0NDAyNGY3NWEyMmU4MTk0OWQ2ODRmMDJmYzE0NGExN2RhYTg2ZjA3MTk2MjZmYjBiMjE2MTdjNWE0NzNmZGJjMjVlODYwMTkwYjZjZjJmMjAyOTBkOTdiZmVkOGNjNWI3MzZmOTdmZGNhNTM5MjA5ZGFiN2E1ZTRiYzM3ZDYwNzk1MTZmOWQ0MTdmMjhiZWFlNjg5MDE5ZGI5MGIwNjA3ZTI0NTcyMTA2MWJkMDY0NWM0ZjI1MmFmNWMzOGY5NTExMWQxZjZiNDkyYzcyN2RlZjFiNjI1ODMzOWMwOGIxNWUxODVmNzVjYjA3NTcxXzdfNGFhOWMxZjc=&#x27;</span></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">keyword = <span class="built_in">input</span>(<span class="string">&#x27;输入要查询的单词：&#x27;</span>)</span><br><span class="line">data = &#123;</span><br><span class="line">    <span class="string">&#x27;from&#x27;</span>: <span class="string">&#x27;en&#x27;</span>,</span><br><span class="line">    <span class="string">&#x27;to&#x27;</span>: <span class="string">&#x27;zh&#x27;</span>,</span><br><span class="line">    <span class="string">&#x27;query&#x27;</span>: <span class="string">&#x27;appl&#x27;</span>,</span><br><span class="line">    <span class="string">&#x27;transtype&#x27;</span>: <span class="string">&#x27;realtime&#x27;</span>,</span><br><span class="line">    <span class="string">&#x27;simple_means_flag&#x27;</span>: <span class="string">&#x27;3&#x27;</span>,</span><br><span class="line">    <span class="string">&#x27;sign&#x27;</span>: <span class="string">&#x27;466982.246551&#x27;</span>,</span><br><span class="line">    <span class="string">&#x27;token&#x27;</span>: <span class="string">&#x27;96365d8cce65e1a98a1638ee4563dc36&#x27;</span>,</span><br><span class="line">    <span class="string">&#x27;domain&#x27;</span>: <span class="string">&#x27;common&#x27;</span>,</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment"># post请求还需要再调用一次encode()方法</span></span><br><span class="line">data = urllib.parse.urlencode(data).encode(<span class="string">&#x27;utf-8&#x27;</span>)</span><br><span class="line"></span><br><span class="line"><span class="comment"># post的请求参数，是不会拼接在url后面的，二十需要放在请求对象定制的参数中</span></span><br><span class="line">request = urllib.request.Request(url=url, data=data, headers=headers)</span><br><span class="line"></span><br><span class="line"><span class="comment"># 模拟浏览器发送请求</span></span><br><span class="line">response = urllib.request.urlopen(request)</span><br><span class="line"></span><br><span class="line"><span class="comment"># 获取响应的数据</span></span><br><span class="line">content = response.read().decode(<span class="string">&#x27;utf-8&#x27;</span>)</span><br><span class="line"></span><br><span class="line"><span class="comment"># 将返回的字符串转换为JSON对象</span></span><br><span class="line"><span class="built_in">print</span>(json.loads(content))</span><br></pre></td></tr></table></figure>
</li>
</ul>
<h4 id="1-3-3、get请求和post请求的区别"><a href="#1-3-3、get请求和post请求的区别" class="headerlink" title="1.3.3、get请求和post请求的区别"></a>1.3.3、get请求和post请求的区别</h4><figure class="highlight stata"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">1：get请求方式的参数必须编码，参数是拼接到url后面，编码之后不需要调用<span class="keyword">encode</span>方法</span><br><span class="line">2：<span class="keyword">post</span>请求方式的参数必须编码，参数是放在请求对象定制的方法中，编码之后需要调用<span class="keyword">encode</span>方法</span><br></pre></td></tr></table></figure>
<h3 id="1-4、Ajax的get请求"><a href="#1-4、Ajax的get请求" class="headerlink" title="1.4、Ajax的get请求"></a>1.4、Ajax的get请求</h3><p><strong>爬取豆瓣电影</strong></p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> urllib.request</span><br><span class="line"><span class="keyword">import</span> urllib.parse</span><br><span class="line"><span class="keyword">import</span> json</span><br><span class="line"></span><br><span class="line"><span class="comment"># 获取内容</span></span><br><span class="line"><span class="keyword">def</span> <span class="title function_">getContent</span>(<span class="params">request</span>):</span><br><span class="line">    <span class="keyword">return</span> urllib.request.urlopen(request).read().decode(<span class="string">&#x27;utf-8&#x27;</span>)</span><br><span class="line"></span><br><span class="line"><span class="comment"># 创建请求对象</span></span><br><span class="line"><span class="keyword">def</span> <span class="title function_">createRequest</span>(<span class="params">page</span>):</span><br><span class="line">    data = &#123;</span><br><span class="line">        <span class="string">&#x27;type&#x27;</span>: <span class="string">&#x27;movie&#x27;</span>,</span><br><span class="line">        <span class="string">&#x27;tag&#x27;</span>: <span class="string">&#x27;热门&#x27;</span>,</span><br><span class="line">        <span class="string">&#x27;sort&#x27;</span>: <span class="string">&#x27;recommend&#x27;</span>,</span><br><span class="line">        <span class="string">&#x27;page_limit&#x27;</span>: page[<span class="string">&#x27;pageSize&#x27;</span>],</span><br><span class="line">        <span class="string">&#x27;page_start&#x27;</span>: page[<span class="string">&#x27;pageNo&#x27;</span>],</span><br><span class="line">    &#125;</span><br><span class="line">    headers = &#123;</span><br><span class="line">        <span class="string">&#x27;User-Agent&#x27;</span>: <span class="string">&#x27;Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/93.0.4577.63 Safari/537.36&#x27;</span>,</span><br><span class="line">    &#125;</span><br><span class="line">    data = urllib.parse.urlencode(data)</span><br><span class="line">    url = <span class="string">&#x27;https://movie.douban.com/j/search_subjects?&#x27;</span> + data</span><br><span class="line">    <span class="keyword">return</span> urllib.request.Request(url=url, headers=headers)</span><br><span class="line"></span><br><span class="line"><span class="comment"># 将数据写入文件中</span></span><br><span class="line"><span class="keyword">def</span> <span class="title function_">getData</span>(<span class="params">page</span>):</span><br><span class="line">    file = <span class="built_in">open</span>(file=<span class="string">&#x27;豆瓣电影.json&#x27;</span>, mode=<span class="string">&#x27;a&#x27;</span>, encoding=<span class="string">&#x27;utf-8&#x27;</span>)</span><br><span class="line">    file.writelines(getContent(createRequest(page)))</span><br><span class="line"></span><br><span class="line"><span class="keyword">if</span> __name__ == <span class="string">&#x27;__main__&#x27;</span>:</span><br><span class="line">    pageNo = <span class="built_in">int</span>(<span class="built_in">input</span>(<span class="string">&#x27;查询多少页：&#x27;</span>))</span><br><span class="line">    pageSize = <span class="built_in">int</span>(<span class="built_in">input</span>(<span class="string">&#x27;每一页查询的数量：&#x27;</span>))</span><br><span class="line"></span><br><span class="line">    <span class="keyword">for</span> i <span class="keyword">in</span> <span class="built_in">range</span>(<span class="number">1</span>, pageNo + <span class="number">1</span>):</span><br><span class="line">        page = &#123;<span class="string">&#x27;pageNo&#x27;</span>: <span class="number">20</span>,<span class="string">&#x27;pageSize&#x27;</span>: <span class="number">20</span>&#125;</span><br><span class="line">        getData(page)</span><br></pre></td></tr></table></figure>
<h3 id="1-5、Ajax的post请求"><a href="#1-5、Ajax的post请求" class="headerlink" title="1.5、Ajax的post请求"></a>1.5、Ajax的post请求</h3><p><strong>爬取肯德基门店地区信息</strong></p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> urllib.parse</span><br><span class="line"><span class="keyword">import</span> urllib.request</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment"># 返回查询内容</span></span><br><span class="line"><span class="keyword">def</span> <span class="title function_">getContent</span>(<span class="params">request</span>):</span><br><span class="line">    <span class="keyword">return</span> urllib.request.urlopen(request).read().decode(<span class="string">&#x27;utf-8&#x27;</span>)</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment"># 创建请求对象</span></span><br><span class="line"><span class="keyword">def</span> <span class="title function_">createRequest</span>(<span class="params">page</span>):</span><br><span class="line">    data = &#123;</span><br><span class="line">        <span class="string">&#x27;cname&#x27;</span>: <span class="string">&#x27;长沙&#x27;</span>,</span><br><span class="line">        <span class="string">&#x27;pid&#x27;</span>: <span class="string">&#x27;&#x27;</span>,</span><br><span class="line">        <span class="string">&#x27;pageSize&#x27;</span>: <span class="built_in">str</span>(page[<span class="string">&#x27;pageSize&#x27;</span>]),</span><br><span class="line">        <span class="string">&#x27;pageIndex&#x27;</span>: <span class="built_in">str</span>(page[<span class="string">&#x27;pageNo&#x27;</span>]),</span><br><span class="line">    &#125;</span><br><span class="line">    headers = &#123;</span><br><span class="line">        <span class="string">&#x27;User-Agent&#x27;</span>: <span class="string">&#x27;Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/93.0.4577.63 Safari/537.36&#x27;</span>,</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="comment"># post请求还需要再调用一次encode()方法</span></span><br><span class="line">    data = urllib.parse.urlencode(data).encode(<span class="string">&#x27;utf-8&#x27;</span>)</span><br><span class="line">    url = <span class="string">&#x27;http://www.kfc.com.cn/kfccda/ashx/GetStoreList.ashx?op=cname&#x27;</span></span><br><span class="line">    <span class="keyword">return</span> urllib.request.Request(url=url, data=data, headers=headers)</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment"># 将书写入文件中</span></span><br><span class="line"><span class="keyword">def</span> <span class="title function_">writeFile</span>(<span class="params">page</span>):</span><br><span class="line">    content = getContent(createRequest(page))</span><br><span class="line">    file = <span class="built_in">open</span>(file=<span class="string">&#x27;肯德基地址信息&#x27;</span> + <span class="built_in">str</span>(page[<span class="string">&#x27;pageNo&#x27;</span>]) + <span class="string">&#x27;.json&#x27;</span>, mode=<span class="string">&#x27;w&#x27;</span>, encoding=<span class="string">&#x27;utf-8&#x27;</span>)</span><br><span class="line">    file.writelines(content)</span><br><span class="line">    file.close()</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="keyword">if</span> __name__ == <span class="string">&#x27;__main__&#x27;</span>:</span><br><span class="line">    <span class="keyword">for</span> i <span class="keyword">in</span> <span class="built_in">range</span>(<span class="number">1</span>, <span class="number">3</span>):</span><br><span class="line">        page = &#123;</span><br><span class="line">            <span class="string">&#x27;pageNo&#x27;</span>: i,</span><br><span class="line">            <span class="string">&#x27;pageSize&#x27;</span>: <span class="number">100</span></span><br><span class="line">        &#125;</span><br><span class="line">        writeFile(page)</span><br></pre></td></tr></table></figure>
<h3 id="1-6、爬虫异常"><a href="#1-6、爬虫异常" class="headerlink" title="1.6、爬虫异常"></a>1.6、爬虫异常</h3><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> urllib.error</span><br><span class="line"></span><br><span class="line"><span class="keyword">try</span>:</span><br><span class="line">    <span class="comment"># 爬虫代码。。。。。</span></span><br><span class="line">    <span class="built_in">print</span>(<span class="string">&#x27;无报错&#x27;</span>)</span><br><span class="line"><span class="keyword">except</span> urllib.error.HTTPError:</span><br><span class="line">    <span class="built_in">print</span>(<span class="string">&#x27;系统正在升级。。。。&#x27;</span>)</span><br><span class="line"><span class="keyword">except</span> urllib.error.URLError:</span><br><span class="line">    <span class="built_in">print</span>(<span class="string">&#x27;系统正在升级。。。。&#x27;</span>)</span><br></pre></td></tr></table></figure>
<h3 id="1-7、cookie登录"><a href="#1-7、cookie登录" class="headerlink" title="1.7、cookie登录"></a>1.7、cookie登录</h3><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 使用场景：数据采集的时候，需要绕过登录，然后进入到某个页面</span></span><br><span class="line"><span class="keyword">import</span> urllib.request</span><br><span class="line"><span class="comment"># 请求地址</span></span><br><span class="line">url = <span class="string">&#x27;https://weibo.com/6487700506/info&#x27;</span></span><br><span class="line"><span class="comment"># 请求头信息</span></span><br><span class="line">headers = &#123;</span><br><span class="line">    <span class="string">&#x27;cookie&#x27;</span>: <span class="string">&#x27;login_sid_t=2c4ccf71ca2a85cf641b07cb4fc1864a; cross_origin_proto=SSL; WBStorage=2ceabba76d81138d|undefined; _s_tentry=-; Apache=7315577089267.8955.1630663188885; SINAGLOBAL=7315577089267.8955.1630663188885; ULV=1630663188889:1:1:1:7315577089267.8955.1630663188885:; WBtopGlobal_register_version=2021090318; crossidccode=CODE-yf-1Mm60G-45S8lz-yJQsRuxArt0NwDz8b1822; SSOLoginState=1630663288; SUB=_2A25MNYIoDeRhGeBK41UW8C7JyzqIHXVv2S5grDV8PUJbkNAKLWvVkW1NR25u8iN5ciVJT-Z-RIv8CaS6M2q1bbrR; SUBP=0033WrSXqPxfM725Ws9jqgMF55529P9D9WhviuqORfY9IB4HpigCERW.5NHD95QcShnNS057SK5cWs4DqcjMi--NiK.Xi-2Ri--ciKnRi-zNSoBRS0M7eh-7Sntt; wvr=6; UOR=,,graph.qq.com; wb_view_log_6487700506=2048*12801.25; webim_unReadCount=%7B%22time%22%3A1630663744090%2C%22dm_pub_total%22%3A0%2C%22chat_group_client%22%3A0%2C%22chat_group_notice%22%3A0%2C%22allcountNum%22%3A40%2C%22msgbox%22%3A0%7D&#x27;</span>,</span><br><span class="line">    <span class="comment"># 防盗链，判断当前路径是不是由上一个路径进来的，一般情况下，是做图片防盗链</span></span><br><span class="line">    <span class="string">&#x27;referer&#x27;</span>: <span class="string">&#x27;https://account.weibo.com/&#x27;</span>,</span><br><span class="line">    <span class="string">&#x27;user-agent&#x27;</span>: <span class="string">&#x27;Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/93.0.4577.63 Safari/537.36&#x27;</span></span><br><span class="line">&#125;</span><br><span class="line"><span class="comment"># 设置请求信息</span></span><br><span class="line">request = urllib.request.Request(url=url, headers=headers)</span><br><span class="line"><span class="comment"># 模拟浏览器访问页面，且获取数据</span></span><br><span class="line">content = urllib.request.urlopen(request).read().decode(<span class="string">&#x27;utf-8&#x27;</span>)</span><br><span class="line"><span class="comment"># 将数据写入文件中</span></span><br><span class="line">file = <span class="built_in">open</span>(file=<span class="string">&#x27;微博账户信息.html&#x27;</span>, mode=<span class="string">&#x27;w&#x27;</span>, encoding=<span class="string">&#x27;utf-8&#x27;</span>)</span><br><span class="line">file.write(content)</span><br><span class="line">file.close()</span><br></pre></td></tr></table></figure>
<h3 id="1-8、Handler处理器"><a href="#1-8、Handler处理器" class="headerlink" title="1.8、Handler处理器"></a>1.8、Handler处理器</h3><ul>
<li>为什么要学习handler？<ul>
<li>urllib.request.urlopen(url)<ul>
<li>不能定制请求头</li>
</ul>
</li>
<li>urllib.request.Request(url,headers,data)<ul>
<li>可以定制请求头</li>
</ul>
</li>
<li>Handler<ul>
<li>定制更高级的请求头（随着业务逻辑的复杂 请求对象的定制已经满足不了我们的需求（动态cookie和代理不能使用请求对象的定制））    </li>
</ul>
</li>
</ul>
</li>
</ul>
<blockquote>
<p><strong>handler的基本使用</strong></p>
</blockquote>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> urllib.request</span><br><span class="line">url = <span class="string">&#x27;http://www.baidu.com&#x27;</span></span><br><span class="line"><span class="comment"># 请求头信息</span></span><br><span class="line">headers = &#123;</span><br><span class="line">    <span class="string">&#x27;user-agent&#x27;</span>: <span class="string">&#x27;Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/93.0.4577.63 Safari/537.36&#x27;</span></span><br><span class="line">&#125;</span><br><span class="line">request = urllib.request.Request(url=url, headers=headers)</span><br><span class="line"></span><br><span class="line"><span class="comment"># handler       build_opener        open</span></span><br><span class="line"><span class="comment"># 1、获取handler对象</span></span><br><span class="line">handler = urllib.request.HTTPHandler()</span><br><span class="line"><span class="comment"># 2、获取opener对象</span></span><br><span class="line">opener = urllib.request.build_opener(handler)</span><br><span class="line"><span class="comment"># 3、调用open方法</span></span><br><span class="line">response = opener.<span class="built_in">open</span>(request)</span><br><span class="line"><span class="comment"># 4、获取数据</span></span><br><span class="line">content = response.read().decode(<span class="string">&#x27;utf-8&#x27;</span>)</span><br></pre></td></tr></table></figure>
<h3 id="1-9、代理服务器"><a href="#1-9、代理服务器" class="headerlink" title="1.9、代理服务器"></a>1.9、代理服务器</h3><ul>
<li>1.代理的常用功能? <ul>
<li>1.突破自身IP访问限制，访问国外站点。 </li>
<li>2.访问一些单位或团体内部资源 <ul>
<li>扩展：某大学FTP(前提是该代理地址在该资源的允许访问范围之内)，使用教育网内地址段免费代理服务 器，就可以用于对教育网开放的各类FTP下载上传，以及各类资料查询共享等服务。 </li>
</ul>
</li>
<li>3.提高访问速度 <ul>
<li>扩展：通常代理服务器都设置一个较大的硬盘缓冲区，当有外界的信息通过时，同时也将其保存到缓冲 区中，当其他用户再访问相同的信息时， 则直接由缓冲区中取出信息，传给用户，以提高访问速度。 </li>
</ul>
</li>
<li>4.隐藏真实IP <ul>
<li>扩展：上网者也可以通过这种方法隐藏自己的IP，免受攻击。 </li>
</ul>
</li>
</ul>
</li>
<li>2.代码配置代理 <ul>
<li>创建Reuqest对象 </li>
<li>创建ProxyHandler对象 用handler对象</li>
<li>创建opener对象 </li>
<li>使用opener.open函数发送请求</li>
</ul>
</li>
</ul>
<h4 id="1-9-1、单个代理"><a href="#1-9-1、单个代理" class="headerlink" title="1.9.1、单个代理"></a>1.9.1、单个代理</h4><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> urllib.request</span><br><span class="line"></span><br><span class="line">url = <span class="string">&#x27;http://www.baidu.com/s?wd=ip&#x27;</span></span><br><span class="line"><span class="comment"># 请求头信息</span></span><br><span class="line">headers = &#123;</span><br><span class="line">    <span class="string">&#x27;User-Agent&#x27;</span>: <span class="string">&#x27;Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/93.0.4577.63 Safari/537.36&#x27;</span></span><br><span class="line">&#125;</span><br><span class="line"><span class="comment"># 请求对象定制</span></span><br><span class="line">request = urllib.request.Request(url=url, headers=headers)</span><br><span class="line"><span class="comment"># 设置代理</span></span><br><span class="line">proxies = &#123;</span><br><span class="line">    <span class="string">&#x27;http&#x27;</span>: <span class="string">&#x27;175.7.199.110:3256&#x27;</span></span><br><span class="line">&#125;</span><br><span class="line"><span class="comment"># handler        build_open      open</span></span><br><span class="line">handler = urllib.request.ProxyHandler(proxies=proxies)</span><br><span class="line">opener = urllib.request.build_opener(handler)</span><br><span class="line"><span class="comment"># # 模拟浏览器访问服务器</span></span><br><span class="line">response = opener.<span class="built_in">open</span>(request)</span><br><span class="line"><span class="comment"># 获取响应的信息</span></span><br><span class="line">content = response.read().decode(<span class="string">&#x27;utf-8&#x27;</span>)</span><br><span class="line"><span class="comment"># 保存在本地</span></span><br><span class="line"><span class="keyword">with</span> <span class="built_in">open</span>(<span class="string">&#x27;代理测试.html&#x27;</span>, <span class="string">&#x27;w&#x27;</span>, encoding=<span class="string">&#x27;utf-8&#x27;</span>)<span class="keyword">as</span> fp:</span><br><span class="line">    fp.write(content)</span><br></pre></td></tr></table></figure>
<h4 id="1-9-2、代理池"><a href="#1-9-2、代理池" class="headerlink" title="1.9.2、代理池"></a>1.9.2、代理池</h4><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> urllib.request</span><br><span class="line"><span class="keyword">import</span> random</span><br><span class="line"></span><br><span class="line">url = <span class="string">&#x27;http://www.baidu.com/s?wd=ip&#x27;</span></span><br><span class="line"><span class="comment"># 请求头信息</span></span><br><span class="line">headers = &#123;</span><br><span class="line">    <span class="string">&#x27;User-Agent&#x27;</span>: <span class="string">&#x27;Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/93.0.4577.63 Safari/537.36&#x27;</span></span><br><span class="line">&#125;</span><br><span class="line"><span class="comment"># 请求对象定制</span></span><br><span class="line">request = urllib.request.Request(url=url, headers=headers)</span><br><span class="line"><span class="comment"># 设置代理池</span></span><br><span class="line">proxiesPool = [</span><br><span class="line">    &#123;<span class="string">&#x27;http&#x27;</span>: <span class="string">&#x27;123.171.42.178:3256&#x27;</span>&#125;,</span><br><span class="line">    &#123;<span class="string">&#x27;http&#x27;</span>: <span class="string">&#x27;163.125.30.152:8118&#x27;</span>&#125;,</span><br><span class="line">    &#123;<span class="string">&#x27;http&#x27;</span>: <span class="string">&#x27;117.94.222.28:3256&#x27;</span>&#125;,</span><br><span class="line">    &#123;<span class="string">&#x27;http&#x27;</span>: <span class="string">&#x27;111.72.25.154:3256&#x27;</span>&#125;,</span><br><span class="line">]</span><br><span class="line"><span class="comment"># handler        build_open      open</span></span><br><span class="line">handler = urllib.request.ProxyHandler(proxies=random.choice(proxiesPool))</span><br><span class="line">opener = urllib.request.build_opener(handler)</span><br><span class="line"><span class="comment"># # 模拟浏览器访问服务器</span></span><br><span class="line">response = opener.<span class="built_in">open</span>(request)</span><br><span class="line"><span class="comment"># 获取响应的信息</span></span><br><span class="line">content = response.read().decode(<span class="string">&#x27;utf-8&#x27;</span>)</span><br><span class="line"><span class="comment"># 保存在本地</span></span><br><span class="line"><span class="keyword">with</span> <span class="built_in">open</span>(<span class="string">&#x27;代理测试.html&#x27;</span>, <span class="string">&#x27;w&#x27;</span>, encoding=<span class="string">&#x27;utf-8&#x27;</span>)<span class="keyword">as</span> fp:</span><br><span class="line">    fp.write(content)</span><br></pre></td></tr></table></figure>
<h2 id="2、解析"><a href="#2、解析" class="headerlink" title="2、解析"></a>2、解析</h2><h3 id="2-1、xpath"><a href="#2-1、xpath" class="headerlink" title="2.1、xpath"></a>2.1、xpath</h3><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1.</span>安装lxml库</span><br><span class="line">	pip install lxml -i https://pypi.douban.com/simple</span><br><span class="line"><span class="number">2.</span>导入lxml.etree</span><br><span class="line">	<span class="keyword">from</span> lxml <span class="keyword">import</span> etree</span><br><span class="line"><span class="number">3.</span>etree.parse() 解析本地文件</span><br><span class="line">	html_tree = etree.parse(<span class="string">&#x27;XX.html&#x27;</span>)</span><br><span class="line"><span class="number">4.</span>etree.HTML() 服务器响应文件</span><br><span class="line">	html_tree = etree.HTML(response.read().decode(<span class="string">&#x27;utf‐8&#x27;</span>)</span><br><span class="line"><span class="number">4.</span>html_tree.xpath(xpath路径)</span><br></pre></td></tr></table></figure>
<h4 id="2-1-1、xpath安装"><a href="#2-1-1、xpath安装" class="headerlink" title="2.1.1、xpath安装"></a>2.1.1、xpath安装</h4><ol>
<li>在谷歌商店中安装<code>xpath</code>插件</li>
<li>安装lxml到python的Scripts的文件夹中【pip install lxml】</li>
</ol>
<h4 id="2-1-2、xpath基本语法"><a href="#2-1-2、xpath基本语法" class="headerlink" title="2.1.2、xpath基本语法"></a>2.1.2、xpath基本语法</h4><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line">xpath基本语法：</span><br><span class="line">	<span class="number">1.</span>路径查询</span><br><span class="line">        //：查找所有子孙节点，不考虑层级关系</span><br><span class="line">        / ：找直接子节点</span><br><span class="line">    <span class="number">2.</span>谓词查询</span><br><span class="line">        //div[@<span class="built_in">id</span>]</span><br><span class="line">        //div[@<span class="built_in">id</span>=<span class="string">&quot;maincontent&quot;</span>]</span><br><span class="line">    <span class="number">3.</span>属性查询,<span class="keyword">class</span>,value,自定义属性</span><br><span class="line">	    //@<span class="keyword">class</span></span><br><span class="line">        //@value</span><br><span class="line">        </span><br><span class="line">    <span class="number">4.</span>模糊查询</span><br><span class="line">        //div[contains(@<span class="built_in">id</span>, <span class="string">&quot;he&quot;</span>)]</span><br><span class="line">        //div[starts‐<span class="keyword">with</span>(@<span class="built_in">id</span>, <span class="string">&quot;he&quot;</span>)]</span><br><span class="line">    <span class="number">5.</span>内容查询</span><br><span class="line">        //div/h1/text()</span><br><span class="line">    <span class="number">6.</span>逻辑运算</span><br><span class="line">        //div[@<span class="built_in">id</span>=<span class="string">&quot;head&quot;</span> <span class="keyword">and</span> @<span class="keyword">class</span>=<span class="string">&quot;s_down&quot;</span>]</span><br><span class="line">        //div[@<span class="built_in">id</span>=<span class="string">&quot;head&quot;</span>] | //div[@<span class="built_in">id</span>=<span class="string">&quot;bottom&quot;</span>]</span><br></pre></td></tr></table></figure>
<blockquote>
<p><strong>解析本地文件</strong></p>
</blockquote>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">from</span> lxml <span class="keyword">import</span> etree</span><br><span class="line"></span><br><span class="line"><span class="comment"># 1、本地文件                                                            etree.parse</span></span><br><span class="line"><span class="comment"># 2、服务器响应的数据（response.read().decode(&#x27;utf-8&#x27;)                      etree.HTML()</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># xpath解析本地文件</span></span><br><span class="line">tree = etree.parse(<span class="string">&#x27;1、xpath的基本使用.html&#x27;</span>)</span><br><span class="line"></span><br><span class="line"><span class="comment"># 路径查询：查找ul下的li。text()获取标签中的内容</span></span><br><span class="line">lis = tree.xpath(<span class="string">&#x27;//body/ul/li&#x27;</span>)</span><br><span class="line"></span><br><span class="line"><span class="comment"># 谓词查询：查询指定标签，且包含id的标签的内容，text()：查看查看标签中的内容</span></span><br><span class="line">liHasId = tree.xpath(<span class="string">&#x27;//ul/li[@id]/text()&#x27;</span>)</span><br><span class="line"><span class="built_in">print</span>(liHasId)</span><br><span class="line"></span><br><span class="line"><span class="comment"># 谓词查询：根据id查找指定标签中的文本值，注意引号</span></span><br><span class="line">liIdIsLi1 = tree.xpath(<span class="string">&#x27;//ul/li[@id=&quot;li1&quot;]/text()&#x27;</span>)</span><br><span class="line"><span class="built_in">print</span>(liIdIsLi1)</span><br><span class="line"></span><br><span class="line"><span class="comment"># 属性查询：根据指定id查找该id下面的查找指定标签的class的值</span></span><br><span class="line">liClassValue = tree.xpath(<span class="string">&#x27;//ul/li[@id=&quot;li1&quot;]/@class&#x27;</span>)</span><br><span class="line"><span class="built_in">print</span>(liClassValue)</span><br><span class="line"></span><br><span class="line"><span class="comment"># 模糊查询：查询指定标签的id值中含有指定字符的内容</span></span><br><span class="line">liCon = tree.xpath(<span class="string">&#x27;//ul/li[contains(@id,&quot;l&quot;)]/text()&#x27;</span>)</span><br><span class="line"><span class="built_in">print</span>(liCon)</span><br><span class="line"></span><br><span class="line"><span class="comment"># 模糊查询：判断id以谁开头的内容</span></span><br><span class="line">idStartWith = tree.xpath(<span class="string">&#x27;//ul/li[starts-with(@id,&quot;a&quot;)]/text()&#x27;</span>)</span><br><span class="line"><span class="built_in">print</span>(idStartWith)</span><br><span class="line"></span><br><span class="line"><span class="comment"># 逻辑运算：查询id和class的值为指定值的内容</span></span><br><span class="line">liByIdAndClass = tree.xpath(<span class="string">&#x27;//ul/li[@id=&quot;li1&quot; and @class=&quot;classValue&quot;]/text()&#x27;</span>)</span><br><span class="line"><span class="built_in">print</span>(liByIdAndClass)</span><br><span class="line"></span><br><span class="line"><span class="comment"># 逻辑运算：查询id或class的值为指定值的内容</span></span><br><span class="line">liByIdOrClass = tree.xpath(<span class="string">&#x27;//ul/li[@id=&quot;li1&quot;]/text() | //ul/li[@id=&quot;li2&quot;]/text()&#x27;</span>)</span><br><span class="line"><span class="built_in">print</span>(liByIdOrClass)</span><br></pre></td></tr></table></figure>
<blockquote>
<p><strong>解析服务器响应的数据</strong></p>
</blockquote>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 1、获取网页的源码</span></span><br><span class="line"><span class="comment"># 2、解析，解析的服务器响应的文件 etree.HTML</span></span><br><span class="line"><span class="comment"># 3、打印</span></span><br><span class="line"><span class="keyword">import</span> urllib.request</span><br><span class="line"><span class="keyword">from</span> lxml <span class="keyword">import</span> etree</span><br><span class="line"></span><br><span class="line">url = <span class="string">&#x27;https://www.baidu.com&#x27;</span></span><br><span class="line">headers = &#123;</span><br><span class="line">    <span class="string">&#x27;User-Agent&#x27;</span>: <span class="string">&#x27;Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/93.0.4577.63 Safari/537.36&#x27;</span></span><br><span class="line">&#125;</span><br><span class="line"><span class="comment"># 请求对象定制</span></span><br><span class="line">request = urllib.request.Request(url=url, headers=headers)</span><br><span class="line"><span class="comment"># 模拟浏览器访问服务器</span></span><br><span class="line">response = urllib.request.urlopen(request)</span><br><span class="line"><span class="comment"># 获取网页源码</span></span><br><span class="line">content = response.read().decode(<span class="string">&#x27;utf-8&#x27;</span>)</span><br><span class="line"><span class="comment"># 解析网页源码，来获取想要的数据，解析服务器响应的文件</span></span><br><span class="line">tree = etree.HTML(content)</span><br><span class="line"><span class="comment"># 获取想要的数据</span></span><br><span class="line">result = tree.xpath(<span class="string">&#x27;//input[@id=&quot;su&quot;]/@value&#x27;</span>)[<span class="number">0</span>]</span><br><span class="line"><span class="built_in">print</span>(result)</span><br></pre></td></tr></table></figure>
<h4 id="2-1-3、xpath基本使用"><a href="#2-1-3、xpath基本使用" class="headerlink" title="2.1.3、xpath基本使用"></a>2.1.3、xpath基本使用</h4><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> urllib.request</span><br><span class="line"><span class="keyword">from</span> lxml <span class="keyword">import</span> etree</span><br><span class="line"></span><br><span class="line"><span class="comment"># 1、请求对象定制</span></span><br><span class="line"><span class="comment"># 2、获取网页的源码</span></span><br><span class="line"><span class="comment"># 3、下载</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># 1、请求对象定制</span></span><br><span class="line"><span class="keyword">def</span> <span class="title function_">createRequest</span>(<span class="params">pageNo</span>):</span><br><span class="line">    url = <span class="string">&quot;&quot;</span></span><br><span class="line">    <span class="keyword">if</span> pageNo == <span class="number">1</span>:</span><br><span class="line">        url = <span class="string">&#x27;https://sc.chinaz.com/tupian/meinvtupian.html&#x27;</span></span><br><span class="line">    <span class="keyword">elif</span> pageNo &gt; <span class="number">1</span>:</span><br><span class="line">        url = <span class="string">&#x27;https://sc.chinaz.com/tupian/meinvtupian_&#x27;</span> + <span class="built_in">str</span>(pageNo) + <span class="string">&#x27;.html&#x27;</span></span><br><span class="line">    headers = &#123;</span><br><span class="line">        <span class="string">&#x27;User-Agent&#x27;</span>: <span class="string">&#x27;Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/93.0.4577.63 Safari/537.36&#x27;</span></span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">return</span> urllib.request.Request(url=url, headers=headers)</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment"># 2、获取网页的源码</span></span><br><span class="line"><span class="keyword">def</span> <span class="title function_">getContent</span>(<span class="params">request</span>):</span><br><span class="line">    response = urllib.request.urlopen(request)</span><br><span class="line">    content = response.read().decode(<span class="string">&#x27;utf-8&#x27;</span>)</span><br><span class="line">    <span class="keyword">return</span> content</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment"># 3、下载</span></span><br><span class="line"><span class="keyword">def</span> <span class="title function_">dowImage</span>(<span class="params">content</span>):</span><br><span class="line">    <span class="comment"># 根据xpath语法解析数据</span></span><br><span class="line">    tree = etree.HTML(content)</span><br><span class="line">    titles = tree.xpath(<span class="string">&#x27;//div[@id=&quot;container&quot;]//a/img/@alt&#x27;</span>)</span><br><span class="line">    srcs = tree.xpath(<span class="string">&#x27;//div[@id=&quot;container&quot;]//a/img/@src2&#x27;</span>)</span><br><span class="line">    items = []</span><br><span class="line">    <span class="keyword">for</span> i <span class="keyword">in</span> <span class="built_in">range</span>(<span class="number">0</span>, <span class="built_in">len</span>(titles)):</span><br><span class="line">        items.append(&#123;<span class="string">&#x27;title&#x27;</span>: titles[i], <span class="string">&#x27;src&#x27;</span>: <span class="string">&quot;https&quot;</span> + srcs[i]&#125;)</span><br><span class="line">    <span class="keyword">for</span> item <span class="keyword">in</span> items:</span><br><span class="line">        <span class="built_in">print</span>(item)</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="keyword">if</span> __name__ == <span class="string">&#x27;__main__&#x27;</span>:</span><br><span class="line">    pageNo = <span class="built_in">int</span>(<span class="built_in">input</span>(<span class="string">&#x27;起始页：&#x27;</span>))</span><br><span class="line">    endPage = <span class="built_in">int</span>(<span class="built_in">input</span>(<span class="string">&#x27;结束页：&#x27;</span>))</span><br><span class="line">    <span class="keyword">for</span> page <span class="keyword">in</span> <span class="built_in">range</span>(pageNo, endPage + <span class="number">1</span>):</span><br><span class="line">        <span class="comment"># 1、请求对象的定制</span></span><br><span class="line">        request = createRequest(page)</span><br><span class="line">        <span class="comment"># 2、获取网页源码</span></span><br><span class="line">        content = getContent(request)</span><br><span class="line">        <span class="comment"># 下载</span></span><br><span class="line">        dowImage(content)</span><br></pre></td></tr></table></figure>
<h3 id="2-2、jsonpath"><a href="#2-2、jsonpath" class="headerlink" title="2.2、jsonpath"></a>2.2、jsonpath</h3><blockquote>
<p><strong>jsonpath安装</strong></p>
</blockquote>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">pip install jsonpath</span><br></pre></td></tr></table></figure>
<blockquote>
<p><strong>jsonpath的基本使用</strong></p>
</blockquote>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> jsonpath</span><br><span class="line"><span class="keyword">import</span> json</span><br><span class="line"><span class="keyword">import</span> urllib.request</span><br><span class="line">	</span><br><span class="line">url = <span class="string">&#x27;https://dianying.taobao.com/cityAction.json?activityId&amp;_ksTS=1630931434381_141&amp;jsoncallback=jsonp142&amp;action=cityAction&amp;n_s=new&amp;event_submit_doGetAllRegion=true&#x27;</span></span><br><span class="line">headers = &#123;</span><br><span class="line">    <span class="string">&#x27;accept&#x27;</span>: <span class="string">&#x27;text/javascript, application/javascript, application/ecmascript, application/x-ecmascript, */*; q=0.01&#x27;</span>,</span><br><span class="line">    <span class="string">&#x27;accept-language&#x27;</span>: <span class="string">&#x27;zh-CN,zh;q=0.9&#x27;</span>,</span><br><span class="line">    <span class="string">&#x27;cookie&#x27;</span>: <span class="string">&#x27;t=719b007b3e7950d336a784bd7b946cc2; td_cookie=1310130070; cookie2=100b92bae0ce9a1a6910e6580c48328a; v=0; _tb_token_=33661457eb3ed; cna=Ng62GZoZwzACAd+YhpEubw0x; xlly_s=1; tfstk=c9GlBQ4CFYyWTR2mCQNSzU6HSppOZf7zfXkEgXKLJTKjgqhVitWVQj86oPno991..; l=eBjfLLvmgM2_tx-XBO5aourza77TBIRb4sPzaNbMiInca6GltF_AvNCKz2qkSdtjgtCffetP_1j3WdLHR3xg5c0c07kqm0Jn3xvO.; isg=BFRUAfoQyTFjK119l_936Tp6JZLGrXiXTdCAjO41GV9i2fQjFr-VJ0xX2dHBIbDv&#x27;</span>,</span><br><span class="line">    <span class="string">&#x27;referer&#x27;</span>: <span class="string">&#x27;https://dianying.taobao.com/&#x27;</span>,</span><br><span class="line">    <span class="string">&#x27;sec-ch-ua&#x27;</span>: <span class="string">&#x27;&quot;Google Chrome&quot;;v=&quot;93&quot;, &quot; Not;A Brand&quot;;v=&quot;99&quot;, &quot;Chromium&quot;;v=&quot;93&quot;&#x27;</span>,</span><br><span class="line">    <span class="string">&#x27;sec-ch-ua-mobile&#x27;</span>: <span class="string">&#x27;?0&#x27;</span>,</span><br><span class="line">    <span class="string">&#x27;sec-ch-ua-platform&#x27;</span>: <span class="string">&#x27;&quot;Windows&quot;&#x27;</span>,</span><br><span class="line">    <span class="string">&#x27;sec-fetch-dest&#x27;</span>: <span class="string">&#x27;empty&#x27;</span>,</span><br><span class="line">    <span class="string">&#x27;sec-fetch-mode&#x27;</span>: <span class="string">&#x27;cors&#x27;</span>,</span><br><span class="line">    <span class="string">&#x27;sec-fetch-site&#x27;</span>: <span class="string">&#x27;same-origin&#x27;</span>,</span><br><span class="line">    <span class="string">&#x27;user-agent&#x27;</span>: <span class="string">&#x27;Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/93.0.4577.63 Safari/537.36&#x27;</span>,</span><br><span class="line">    <span class="string">&#x27;x-requested-with&#x27;</span>: <span class="string">&#x27;XMLHttpRequest&#x27;</span></span><br><span class="line">&#125;</span><br><span class="line">request = urllib.request.Request(url=url, headers=headers)</span><br><span class="line">response = urllib.request.urlopen(request)</span><br><span class="line">content = response.read().decode(<span class="string">&#x27;utf-8&#x27;</span>)</span><br><span class="line">content = content.split(<span class="string">&#x27;(&#x27;</span>)[<span class="number">1</span>].split(<span class="string">&#x27;)&#x27;</span>)[<span class="number">0</span>]</span><br><span class="line">file = <span class="built_in">open</span>(<span class="string">&#x27;2、jsonpath解析淘票票.json&#x27;</span>, <span class="string">&#x27;w&#x27;</span>, encoding=<span class="string">&#x27;utf-8&#x27;</span>)</span><br><span class="line">file.write(content)</span><br><span class="line">file.close()</span><br><span class="line"></span><br><span class="line">obj = json.load(<span class="built_in">open</span>(<span class="string">&#x27;2、jsonpath解析淘票票.json&#x27;</span>, <span class="string">&#x27;r&#x27;</span>, encoding=<span class="string">&#x27;utf-8&#x27;</span>))</span><br><span class="line">name = jsonpath.jsonpath(obj, <span class="string">&#x27;$.returnValue.A[0].regionName&#x27;</span>)</span><br><span class="line"><span class="built_in">print</span>(name)</span><br></pre></td></tr></table></figure>
<h3 id="2-3、-BeautifulSoup"><a href="#2-3、-BeautifulSoup" class="headerlink" title="2.3、.BeautifulSoup"></a>2.3、.BeautifulSoup</h3><h4 id="2-3-1、安装及简介"><a href="#2-3-1、安装及简介" class="headerlink" title="2.3.1、安装及简介"></a>2.3.1、安装及简介</h4><blockquote>
<p><strong>基本简介</strong></p>
</blockquote>
<ul>
<li>BeautifulSoup简称： bs4 </li>
<li>什么是BeatifulSoup？ <ul>
<li>BeautifulSoup，和lxml一样，是一个html的解析器，主要功能也是解析和提取数据 </li>
</ul>
</li>
<li>优缺点？ <ul>
<li>缺点：效率没有lxml的效率高 </li>
<li>优点：接口设计人性化，使用方便</li>
</ul>
</li>
</ul>
<blockquote>
<p><strong>安装及其创建</strong></p>
</blockquote>
<ul>
<li>安装<ul>
<li>pip install bs4</li>
</ul>
</li>
<li>导入<ul>
<li>from bs4 import BeautifulSoup</li>
</ul>
</li>
<li>创建对象<ul>
<li>服务器响应的文件生成对象<ul>
<li>soup = BeautifulSoup(response.read().decode(), ‘lxml’)</li>
</ul>
</li>
<li>本地文件生成对象<ul>
<li>soup = BeautifulSoup(open(‘1.html’), ‘lxml’) </li>
<li>注意：默认打开文件的编码格式gbk所以需要指定打开编码格式</li>
</ul>
</li>
</ul>
</li>
</ul>
<h4 id="2-3-2、bs4的基本使用"><a href="#2-3-2、bs4的基本使用" class="headerlink" title="2.3.2、bs4的基本使用"></a>2.3.2、bs4的基本使用</h4><p><strong>格式：</strong></p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1.</span>根据标签名查找节点</span><br><span class="line">    soup.a 【注】只能找到第一个a</span><br><span class="line">        soup.a.name</span><br><span class="line">        soup.a.attrs</span><br><span class="line"><span class="number">2.</span>函数</span><br><span class="line">    (<span class="number">1</span>).find(返回一个对象)</span><br><span class="line"> 	   	find(<span class="string">&#x27;a&#x27;</span>)：只找到第一个a标签</span><br><span class="line">       	find(<span class="string">&#x27;a&#x27;</span>, title=<span class="string">&#x27;名字&#x27;</span>)</span><br><span class="line">		find(<span class="string">&#x27;a&#x27;</span>, class_=<span class="string">&#x27;名字&#x27;</span>)</span><br><span class="line">    (<span class="number">2</span>).find_all(返回一个列表)</span><br><span class="line">        find_all(<span class="string">&#x27;a&#x27;</span>) 查找到所有的a</span><br><span class="line">        find_all([<span class="string">&#x27;a&#x27;</span>, <span class="string">&#x27;span&#x27;</span>]) 返回所有的a和span</span><br><span class="line">        find_all(<span class="string">&#x27;a&#x27;</span>, limit=<span class="number">2</span>) 只找前两个a</span><br><span class="line">    (<span class="number">3</span>).select(根据选择器得到节点对象)【推荐】</span><br><span class="line">        <span class="number">1.</span>element</span><br><span class="line">        	eg:p</span><br><span class="line">        <span class="number">2.</span>.<span class="keyword">class</span></span><br><span class="line">        	<span class="title class_">eg</span>:.firstname</span><br><span class="line">        <span class="number">3.</span><span class="comment">#id</span></span><br><span class="line">        	eg:<span class="comment">#firstname</span></span><br><span class="line">        <span class="number">4.</span>属性选择器</span><br><span class="line">        	[attribute]</span><br><span class="line">        		eg:li = soup.select(<span class="string">&#x27;li[class]&#x27;</span>)</span><br><span class="line">        	[attribute=value]</span><br><span class="line">       			eg:li = soup.select(<span class="string">&#x27;li[class=&quot;hengheng1&quot;]&#x27;</span>)</span><br><span class="line">        <span class="number">5.</span>层级选择器</span><br><span class="line">            element element</span><br><span class="line">            	div p</span><br><span class="line">            element&gt;element</span><br><span class="line">            	div&gt;p</span><br><span class="line">            element,element</span><br><span class="line">            	div,p</span><br><span class="line">            		eg:soup = soup.select(<span class="string">&#x27;a,span&#x27;</span>)</span><br></pre></td></tr></table></figure>
<p><strong>示例</strong></p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">ul</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">li</span>&gt;</span><span class="tag">&lt;<span class="name">a</span> <span class="attr">href</span>=<span class="string">&quot;#&quot;</span> <span class="attr">title</span>=<span class="string">&quot;测试&quot;</span> <span class="attr">class</span>=<span class="string">&quot;aaa&quot;</span>&gt;</span>测试<span class="tag">&lt;/<span class="name">a</span>&gt;</span><span class="tag">&lt;/<span class="name">li</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">li</span>&gt;</span><span class="tag">&lt;<span class="name">a</span> <span class="attr">href</span>=<span class="string">&quot;21321&quot;</span> <span class="attr">title</span>=<span class="string">&quot;测试2&quot;</span> <span class="attr">class</span>=<span class="string">&quot;bbb&quot;</span>&gt;</span>测试2<span class="tag">&lt;/<span class="name">a</span>&gt;</span><span class="tag">&lt;/<span class="name">li</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">li</span>&gt;</span><span class="tag">&lt;<span class="name">a</span> <span class="attr">href</span>=<span class="string">&quot;#&quot;</span>&gt;</span><span class="tag">&lt;<span class="name">span</span>&gt;</span>测试一下<span class="tag">&lt;/<span class="name">span</span>&gt;</span><span class="tag">&lt;/<span class="name">a</span>&gt;</span><span class="tag">&lt;/<span class="name">li</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">li</span>&gt;</span><span class="tag">&lt;<span class="name">a</span> <span class="attr">href</span>=<span class="string">&quot;##&quot;</span> <span class="attr">id</span>=<span class="string">&quot;ccc&quot;</span>&gt;</span><span class="tag">&lt;/<span class="name">a</span>&gt;</span><span class="tag">&lt;/<span class="name">li</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">ul</span>&gt;</span></span><br></pre></td></tr></table></figure>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">from</span> bs4 <span class="keyword">import</span> BeautifulSoup</span><br><span class="line"></span><br><span class="line"><span class="comment"># 通过解析本地文件，来将bs4的基础语法进行解析</span></span><br><span class="line">soup = BeautifulSoup(<span class="built_in">open</span>(<span class="string">&#x27;1、bs4的基本使用.html&#x27;</span>, <span class="string">&#x27;r&#x27;</span>, encoding=<span class="string">&#x27;utf-8&#x27;</span>), <span class="string">&#x27;lxml&#x27;</span>)</span><br><span class="line"></span><br><span class="line"><span class="comment"># 根据标签名查找节点</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># 查找第一个符合条件的数据</span></span><br><span class="line"><span class="built_in">print</span>(soup.a)</span><br><span class="line"><span class="comment"># 获取标签的属性和属性值</span></span><br><span class="line"><span class="built_in">print</span>(soup.a.attrs)</span><br><span class="line"></span><br><span class="line"><span class="comment"># bs4的一些函数</span></span><br><span class="line"><span class="comment"># find find_all select</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># find：返回的是第一个符合条件的数据</span></span><br><span class="line"><span class="built_in">print</span>(soup.find(<span class="string">&#x27;a&#x27;</span>))</span><br><span class="line"><span class="built_in">print</span>(soup.find(<span class="string">&#x27;a&#x27;</span>, title=<span class="string">&#x27;测试2&#x27;</span>))  <span class="comment"># 根据属性的值来查找数据</span></span><br><span class="line"><span class="built_in">print</span>(soup.find(<span class="string">&#x27;a&#x27;</span>, class_=<span class="string">&#x27;aaa&#x27;</span>))  <span class="comment"># 根据class的值来找到对应的标签对象。注意：class要写成class_</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># find_all：返回所有满足条件的数据，返回一个列表</span></span><br><span class="line"><span class="built_in">print</span>(soup.find_all(<span class="string">&#x27;a&#x27;</span>))</span><br><span class="line"><span class="built_in">print</span>(soup.find_all(<span class="string">&#x27;a&#x27;</span>, class_=<span class="string">&#x27;bbb&#x27;</span>))</span><br><span class="line"><span class="comment"># 想要获取多个标签的数据，需要在find_all的参数中添加的是列表的数据</span></span><br><span class="line"><span class="built_in">print</span>(soup.find_all([<span class="string">&#x27;a&#x27;</span>, <span class="string">&#x27;span&#x27;</span>]))</span><br><span class="line"><span class="built_in">print</span>(soup.find_all(<span class="string">&#x27;li&#x27;</span>, limit=<span class="number">1</span>))  <span class="comment"># limit：用于指定查找前几个的数据</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># select(推荐)：返回的是一个列表，并且返回多个数据</span></span><br><span class="line"><span class="built_in">print</span>(<span class="string">&quot;=====================select=========================&quot;</span>)</span><br><span class="line"><span class="built_in">print</span>(soup.select(<span class="string">&#x27;a&#x27;</span>))</span><br><span class="line"><span class="built_in">print</span>(soup.select(<span class="string">&#x27;a[class=&quot;aaa&quot;]&#x27;</span>))  <span class="comment"># 根据class选择器来选中</span></span><br></pre></td></tr></table></figure>
<h4 id="2-3-3、节点信息-获取标签内容"><a href="#2-3-3、节点信息-获取标签内容" class="headerlink" title="2.3.3、节点信息-获取标签内容"></a>2.3.3、节点信息-获取标签内容</h4><blockquote>
<p><strong>基本使用</strong></p>
</blockquote>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">from</span> bs4 <span class="keyword">import</span> BeautifulSoup</span><br><span class="line"></span><br><span class="line">soup = BeautifulSoup(<span class="built_in">open</span>(<span class="string">&#x27;1、bs4的基本使用.html&#x27;</span>, <span class="string">&#x27;r&#x27;</span>, encoding=<span class="string">&#x27;utf-8&#x27;</span>), <span class="string">&#x27;lxml&#x27;</span>)</span><br><span class="line">obj = soup.select(<span class="string">&#x27;#div1&#x27;</span>)[<span class="number">0</span>]</span><br><span class="line"></span><br><span class="line"><span class="number">1</span>、获取节点内容：适用于标签中嵌套标签的结构</span><br><span class="line">	obj.string</span><br><span class="line">    obj.get_text()【推荐】</span><br><span class="line"><span class="number">2</span>、节点的属性</span><br><span class="line">	tag.name 获取标签名</span><br><span class="line">    	eg:tag = find(<span class="string">&#x27;li&#x27;</span>)</span><br><span class="line">			<span class="built_in">print</span>(tag.name) <span class="comment"># 输出：li</span></span><br><span class="line">		tag.attrs将属性值作为一个字典返回【属性名:属性值】</span><br><span class="line"><span class="number">3</span>、获取节点属性：因为obj.attrs返回一个字典，可以直接使用字典的方法le</span><br><span class="line">    obj.attrs.get(<span class="string">&#x27;title&#x27;</span>)【常用】</span><br><span class="line">    obj.get(<span class="string">&#x27;title&#x27;</span>)</span><br><span class="line">    obj[<span class="string">&#x27;title&#x27;</span>]</span><br></pre></td></tr></table></figure>
<blockquote>
<p><strong>示例</strong></p>
</blockquote>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">div</span> <span class="attr">id</span>=<span class="string">&quot;div1&quot;</span>&gt;</span><span class="tag">&lt;<span class="name">span</span>&gt;</span>测试一下<span class="tag">&lt;/<span class="name">span</span>&gt;</span><span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br></pre></td></tr></table></figure>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">from</span> bs4 <span class="keyword">import</span> BeautifulSoup</span><br><span class="line"></span><br><span class="line"><span class="comment"># 通过解析本地文件，来将bs4的基础语法进行解析</span></span><br><span class="line">soup = BeautifulSoup(<span class="built_in">open</span>(<span class="string">&#x27;1、bs4的基本使用.html&#x27;</span>, <span class="string">&#x27;r&#x27;</span>, encoding=<span class="string">&#x27;utf-8&#x27;</span>), <span class="string">&#x27;lxml&#x27;</span>)</span><br><span class="line"></span><br><span class="line"><span class="comment"># 节点信息，获取节点内容</span></span><br><span class="line"><span class="comment"># 如果标签对象中，除了内容还有标签，那么string就获取不到数据，而get_text()是可以获取数据</span></span><br><span class="line"><span class="comment"># 一般情况下，推荐使用get_text()</span></span><br><span class="line">textValue = soup.select(<span class="string">&#x27;#div1&#x27;</span>)[<span class="number">0</span>]</span><br><span class="line"><span class="built_in">print</span>(textValue.value)  <span class="comment"># None</span></span><br><span class="line"><span class="built_in">print</span>(textValue.get_text())  <span class="comment"># 测试一下</span></span><br><span class="line"><span class="built_in">print</span>(textValue.attrs)  <span class="comment"># &#123;&#x27;id&#x27;: &#x27;div1&#x27;&#125;</span></span><br><span class="line"><span class="built_in">print</span>(textValue.attrs.get(<span class="string">&#x27;id&#x27;</span>))  <span class="comment"># div1</span></span><br></pre></td></tr></table></figure>
<blockquote>
<p><strong>使用bs4获取星巴克数据</strong></p>
</blockquote>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> urllib.request</span><br><span class="line"><span class="keyword">from</span> bs4 <span class="keyword">import</span> BeautifulSoup</span><br><span class="line"></span><br><span class="line">url = <span class="string">&#x27;https://www.starbucks.com.cn/menu/&#x27;</span></span><br><span class="line">response = urllib.request.urlopen(url=url)</span><br><span class="line">content = response.read().decode(<span class="string">&#x27;utf-8&#x27;</span>)</span><br><span class="line"></span><br><span class="line">soup = BeautifulSoup(content, <span class="string">&#x27;lxml&#x27;</span>)</span><br><span class="line"></span><br><span class="line">names = soup.select(<span class="string">&#x27;ul[class=&quot;grid padded-3 product&quot;] strong&#x27;</span>)</span><br><span class="line"></span><br><span class="line"><span class="keyword">for</span> name <span class="keyword">in</span> names:</span><br><span class="line">    <span class="built_in">print</span>(name.get_text().replace(<span class="string">&#x27;™&#x27;</span>, <span class="string">&#x27;&#x27;</span>))</span><br></pre></td></tr></table></figure>
<h2 id="3、Selenium"><a href="#3、Selenium" class="headerlink" title="3、Selenium"></a>3、Selenium</h2><h3 id="3-1、selenium"><a href="#3-1、selenium" class="headerlink" title="3.1、selenium"></a>3.1、selenium</h3><h4 id="3-1-1、Seleneum基本使用"><a href="#3-1-1、Seleneum基本使用" class="headerlink" title="3.1.1、Seleneum基本使用"></a>3.1.1、Seleneum基本使用</h4><ol>
<li>什么是selenium是一个用于web程序<ul>
<li>Selenium是一个用于Web应用程序测试的工具。</li>
<li>Selenium 测试直接运行在浏览器中，就像真正的用户在操作一样。</li>
<li>支持通过各种driver（FirfoxDriver，IternetExplorerDriver，OperaDriver，ChromeDriver）驱动 真实浏览器完成测试。</li>
<li>selenium也是支持无界面浏览器操作的</li>
</ul>
</li>
<li>为什么使用selenium？ <ul>
<li>模拟浏览器功能，自动执行网页中的js代码，实现动态加载</li>
</ul>
</li>
<li>如何安装selenium？ <ul>
<li>操作谷歌浏览器驱动下载地址 <a target="_blank" rel="noopener" href="http://chromedriver.storage.googleapis.com/index.html">http://chromedriver.storage.googleapis.com/index.html</a> </li>
<li>谷歌驱动和谷歌浏览器版本之间的映射表 <a target="_blank" rel="noopener" href="http://blog.csdn.net/huilan_same/article/details/51896672">http://blog.csdn.net/huilan_same/article/details/51896672</a> </li>
<li>查看谷歌浏览器版本 谷歌浏览器右上角‐‐&gt;帮助‐‐&gt;关于 </li>
<li>pip install selenium</li>
</ul>
</li>
<li>selenium的使用步骤？ <ul>
<li>导入：from selenium import webdriver</li>
<li>创建谷歌浏览器操作对象： path = 谷歌浏览器驱动文件路径 browser = webdriver.Chrome(path) </li>
<li>访问网址 url = 要访问的网址 browser.get(url)</li>
</ul>
</li>
</ol>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 1、导入selenium</span></span><br><span class="line"><span class="keyword">from</span> selenium <span class="keyword">import</span> webdriver</span><br><span class="line"></span><br><span class="line"><span class="comment"># 2、创建浏览器操作对象</span></span><br><span class="line">path = <span class="string">&#x27;chromedriver.exe&#x27;</span>  <span class="comment"># 浏览器驱动的路径</span></span><br><span class="line">browser = webdriver.Chrome(path)</span><br><span class="line"></span><br><span class="line"><span class="comment"># 3、访问网址</span></span><br><span class="line">url = <span class="string">&#x27;https://www.jd.com/&#x27;</span></span><br><span class="line">browser.get(url)</span><br><span class="line"></span><br><span class="line"><span class="comment"># page_source获取网页源码</span></span><br><span class="line">content = browser.page_source</span><br><span class="line"><span class="built_in">print</span>(content)</span><br></pre></td></tr></table></figure>
<h4 id="3-1-2、Selenium元素定位"><a href="#3-1-2、Selenium元素定位" class="headerlink" title="3.1.2、Selenium元素定位"></a>3.1.2、Selenium元素定位</h4><blockquote>
<p><strong>api</strong></p>
</blockquote>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1.</span>find_element_by_id <span class="comment">#【根据id查询】</span></span><br><span class="line">	eg:button = browser.find_element_by_id(<span class="string">&#x27;su&#x27;</span>)</span><br><span class="line"><span class="number">2.</span>find_elements_by_name <span class="comment"># 根据name属性的值</span></span><br><span class="line">	eg:name = browser.find_element_by_name(<span class="string">&#x27;wd&#x27;</span>)</span><br><span class="line"><span class="number">3.</span>find_elements_by_xpath <span class="comment"># 根据xpath语法</span></span><br><span class="line">	eg:xpath1 = browser.find_elements_by_xpath(<span class="string">&#x27;//input[@id=&quot;su&quot;]&#x27;</span>)</span><br><span class="line"><span class="number">4.</span>find_elements_by_tag_name <span class="comment"># 根据标签名字</span></span><br><span class="line">	eg:names = browser.find_elements_by_tag_name(<span class="string">&#x27;input&#x27;</span>)</span><br><span class="line"><span class="number">5.</span>find_elements_by_css_selector <span class="comment"># 根据css选择器</span></span><br><span class="line">	eg:my_input = browser.find_elements_by_css_selector(<span class="string">&#x27;#kw&#x27;</span>)[<span class="number">0</span>]</span><br><span class="line"><span class="number">6.</span>find_elements_by_link_text <span class="comment"># 根据连接文本</span></span><br><span class="line">	eg:browser.find_element_by_link_text(<span class="string">&quot;新闻&quot;</span>)</span><br></pre></td></tr></table></figure>
<blockquote>
<p><strong>示例</strong></p>
</blockquote>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">from</span> selenium <span class="keyword">import</span> webdriver</span><br><span class="line"></span><br><span class="line">path = <span class="string">&#x27;chromedriver.exe&#x27;</span></span><br><span class="line">browser = webdriver.Chrome(path)</span><br><span class="line"></span><br><span class="line">url = <span class="string">&#x27;https://www.baidu.com/&#x27;</span></span><br><span class="line">browser.get(url)</span><br><span class="line"></span><br><span class="line"><span class="comment"># 元素定位</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># find_element_by_id</span></span><br><span class="line">respById = browser.find_element_by_id(<span class="string">&#x27;su&#x27;</span>)</span><br><span class="line"></span><br><span class="line"><span class="comment"># 根据标签属性的属性值来获取对象，根据name属性的值来获取对象</span></span><br><span class="line">respByName = browser.find_element_by_name(<span class="string">&#x27;wd&#x27;</span>)</span><br><span class="line"></span><br><span class="line"><span class="comment"># 根据xpath语句来获取对象</span></span><br><span class="line">respByXpath = browser.find_element_by_xpath(<span class="string">&#x27;//input[@id=&quot;su&quot;]&#x27;</span>)</span><br><span class="line"></span><br><span class="line"><span class="comment"># 根据标签名来获取对象</span></span><br><span class="line">respByTagName = browser.find_elements_by_tag_name(<span class="string">&#x27;input&#x27;</span>)</span><br><span class="line"></span><br><span class="line"><span class="comment"># 使用css选择器</span></span><br><span class="line">respByCss = browser.find_element_by_css_selector(<span class="string">&#x27;#su&#x27;</span>)</span><br><span class="line"></span><br><span class="line"><span class="comment"># 根据连接文本的值来获取标签，比如：a标签就是连接文本</span></span><br><span class="line">respByLink = browser.find_element_by_link_text(<span class="string">&#x27;地图&#x27;</span>)</span><br></pre></td></tr></table></figure>
<h4 id="3-1-3、selenium访问元素信息"><a href="#3-1-3、selenium访问元素信息" class="headerlink" title="3.1.3、selenium访问元素信息"></a>3.1.3、selenium访问元素信息</h4><blockquote>
<p><strong>方法</strong></p>
</blockquote>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">获取元素信息：【 .get_attribute 】</span><br><span class="line">获取标签之间的文本信息：【 .text 】</span><br><span class="line">获取标签的名字：【 .tag_name 】</span><br></pre></td></tr></table></figure>
<blockquote>
<p><strong>示例</strong></p>
</blockquote>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">from</span> selenium <span class="keyword">import</span> webdriver</span><br><span class="line"></span><br><span class="line">path = <span class="string">&#x27;chromedriver.exe&#x27;</span></span><br><span class="line">browser = webdriver.Chrome(path)</span><br><span class="line"></span><br><span class="line">url = <span class="string">&#x27;https://www.baidu.com/&#x27;</span></span><br><span class="line">browser.get(url)</span><br><span class="line"></span><br><span class="line">respById = browser.find_element_by_id(<span class="string">&#x27;su&#x27;</span>)</span><br><span class="line"><span class="built_in">print</span>(respById.get_attribute(<span class="string">&#x27;class&#x27;</span>))  <span class="comment"># 获取指定属性的值</span></span><br><span class="line"><span class="built_in">print</span>(respById.tag_name)  <span class="comment"># 获取标签名字</span></span><br><span class="line"><span class="built_in">print</span>(respById.text) <span class="comment"># 获取标签之间的文本信息</span></span><br><span class="line"><span class="built_in">print</span>(respById.get_attribute(<span class="string">&#x27;value&#x27;</span>))  <span class="comment"># 获取双标签之间的文本</span></span><br></pre></td></tr></table></figure>
<h4 id="3-1-4、selenium交互"><a href="#3-1-4、selenium交互" class="headerlink" title="3.1.4、selenium交互"></a>3.1.4、selenium交互</h4><p><strong>selenium交互就是模拟用户操作浏览器，点击按钮，上一页，往文本框中输入信息等操作</strong></p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">from</span> selenium <span class="keyword">import</span> webdriver</span><br><span class="line"><span class="keyword">import</span> time</span><br><span class="line"></span><br><span class="line">path = <span class="string">&#x27;chromedriver.exe&#x27;</span></span><br><span class="line">browser = webdriver.Chrome(path)</span><br><span class="line">url = <span class="string">&#x27;https://www.baidu.com/&#x27;</span></span><br><span class="line"></span><br><span class="line">browser.get(url)</span><br><span class="line"></span><br><span class="line">time.sleep(<span class="number">2</span>)  <span class="comment"># 睡眠两秒</span></span><br><span class="line">respById = browser.find_element_by_id(<span class="string">&#x27;kw&#x27;</span>)  <span class="comment"># 获取文本框的对象</span></span><br><span class="line">respById.send_keys(<span class="string">&#x27;周杰伦&#x27;</span>)  <span class="comment"># 文本框中输入周杰伦</span></span><br><span class="line">time.sleep(<span class="number">2</span>)  <span class="comment"># 睡眠两秒</span></span><br><span class="line">respByIdBtn = browser.find_element_by_id(<span class="string">&#x27;su&#x27;</span>)  <span class="comment"># 点击百度一下</span></span><br><span class="line">respByIdBtn.click()  <span class="comment"># 点击按钮</span></span><br><span class="line">time.sleep(<span class="number">2</span>)  <span class="comment"># 睡眠两秒</span></span><br><span class="line">toBottom = <span class="string">&#x27;document.documentElement.scrollTop=100000&#x27;</span>  <span class="comment"># 滑到底部</span></span><br><span class="line">browser.execute_script(toBottom)  <span class="comment"># 滑到底部</span></span><br><span class="line">time.sleep(<span class="number">2</span>)  <span class="comment"># 睡眠两秒</span></span><br><span class="line">nextBtn = browser.find_element_by_xpath(<span class="string">&#x27;//a[@class=&quot;n&quot;]&#x27;</span>)  <span class="comment"># 获取下一页按钮</span></span><br><span class="line">nextBtn.click()  <span class="comment"># 点击下一页</span></span><br><span class="line">time.sleep(<span class="number">2</span>)  <span class="comment"># 睡眠两秒</span></span><br><span class="line">browser.back()  <span class="comment"># 回到上一页</span></span><br><span class="line">time.sleep(<span class="number">2</span>)  <span class="comment"># 睡眠两秒</span></span><br><span class="line">browser.forward()  <span class="comment"># 回到之前页面</span></span><br><span class="line">browser.quit()  <span class="comment"># 退出</span></span><br></pre></td></tr></table></figure>
<h3 id="3-2、Phantomjs"><a href="#3-2、Phantomjs" class="headerlink" title="3.2、Phantomjs"></a>3.2、Phantomjs</h3><blockquote>
<p><strong>下载</strong></p>
</blockquote>
<p><a target="_blank" rel="noopener" href="https://phantomjs.org/download.html">https://phantomjs.org/download.html</a></p>
<blockquote>
<p><strong>使用</strong></p>
</blockquote>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">from</span> selenium <span class="keyword">import</span> webdriver</span><br><span class="line"></span><br><span class="line">path = <span class="string">&#x27;phantomjs.exe&#x27;</span></span><br><span class="line">browser = webdriver.PhantomJS(path)</span><br><span class="line"></span><br><span class="line">url = <span class="string">&#x27;https://www.baidu.com/&#x27;</span></span><br><span class="line">browser.get(url)</span><br><span class="line">browser.save_screenshot(<span class="string">&#x27;百度首页截图.webp&#x27;</span>)  <span class="comment"># 截图</span></span><br></pre></td></tr></table></figure>
<h3 id="3-3、Chrome-handless"><a href="#3-3、Chrome-handless" class="headerlink" title="3.3、Chrome handless"></a>3.3、Chrome handless</h3><p><strong>Chrome-headless 模式， Google 针对 Chrome 浏览器 59版 新增加的一种模式，可以让你不打开UI界面的情况下 使用 Chrome 浏览器，所以运行效果与 Chrome 保持完美一致。</strong></p>
<blockquote>
<p><strong>系统要求</strong></p>
</blockquote>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1.</span>系统要求：</span><br><span class="line">    Chrome</span><br><span class="line">        Unix\Linux 系统需要 chrome &gt;= <span class="number">59</span></span><br><span class="line">        Windows 系统需要 chrome &gt;= <span class="number">60</span></span><br><span class="line">    Python3<span class="number">.6</span></span><br><span class="line">    Selenium==<span class="number">3.4</span>.*</span><br><span class="line">    ChromeDriver==<span class="number">2.31</span></span><br></pre></td></tr></table></figure>
<blockquote>
<p><strong>配置和使用</strong></p>
</blockquote>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">from</span> selenium <span class="keyword">import</span> webdriver</span><br><span class="line"><span class="keyword">from</span> selenium.webdriver.chrome.options <span class="keyword">import</span> Options</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment"># 封装的handlers</span></span><br><span class="line"><span class="keyword">def</span> <span class="title function_">shareBrowser</span>():</span><br><span class="line">    chrome_options = Options()</span><br><span class="line">    chrome_options.add_argument(<span class="string">&#x27;--headless&#x27;</span>)</span><br><span class="line">    chrome_options.add_argument(<span class="string">&#x27;--disable‐gpu&#x27;</span>)</span><br><span class="line"></span><br><span class="line">    path = <span class="string">r&#x27;C:\Program Files\Google\Chrome\Application\chrome.exe&#x27;</span></span><br><span class="line">    chrome_options.binary_location = path</span><br><span class="line"></span><br><span class="line">    browser = webdriver.Chrome(options=chrome_options)</span><br><span class="line">    <span class="keyword">return</span> browser</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">browser = shareBrowser()</span><br><span class="line">url = <span class="string">&#x27;https://www.baidu.com/&#x27;</span></span><br><span class="line">browser.get(url)</span><br><span class="line">browser.save_screenshot(<span class="string">&#x27;百度首页.webp&#x27;</span>)</span><br></pre></td></tr></table></figure>
<h2 id="4、requests"><a href="#4、requests" class="headerlink" title="4、requests"></a>4、requests</h2><h3 id="4-1、基本使用"><a href="#4-1、基本使用" class="headerlink" title="4.1、基本使用"></a>4.1、基本使用</h3><blockquote>
<p><strong>文档</strong></p>
</blockquote>
<p>官网：<a target="_blank" rel="noopener" href="https://docs.python-requests.org/zh_CN/latest/">https://docs.python-requests.org/zh_CN/latest/</a></p>
<p>快速上手：<a target="_blank" rel="noopener" href="https://docs.python-requests.org/zh_CN/latest/user/quickstart.html">https://docs.python-requests.org/zh_CN/latest/user/quickstart.html</a></p>
<blockquote>
<p><strong>安装</strong></p>
</blockquote>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">pip install requests</span><br></pre></td></tr></table></figure>
<blockquote>
<p><strong>responses基本属性</strong></p>
</blockquote>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">3.</span>response的属性以及类型</span><br><span class="line">    类型  		  :models.Response</span><br><span class="line">    r.text         :获取网站源码</span><br><span class="line">    r.encoding 	   :访问或定制编码方式</span><br><span class="line">    r.url          :获取请求的url</span><br><span class="line">    r.content      :响应的字节类型</span><br><span class="line">    r.status_code  :响应的状态码</span><br><span class="line">    r.headers      :响应的头信息</span><br></pre></td></tr></table></figure>
<blockquote>
<p><strong>使用【一个类型和六个方法】</strong></p>
</blockquote>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> requests</span><br><span class="line"></span><br><span class="line">url = <span class="string">&#x27;https://www.baidu.com/&#x27;</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># 一个类型和六个属性</span></span><br><span class="line">response = requests.get(url=url)</span><br><span class="line"><span class="built_in">print</span>(<span class="built_in">type</span>(response))  <span class="comment"># 一个类型</span></span><br><span class="line">response.encoding = <span class="string">&#x27;utf-8&#x27;</span>  <span class="comment"># 属性：访问或定制编码方式</span></span><br><span class="line"><span class="built_in">print</span>(response.url)  <span class="comment"># 属性：获取请求的url</span></span><br><span class="line"><span class="built_in">print</span>(response.content)  <span class="comment"># 属性：响应的字节类型</span></span><br><span class="line"><span class="built_in">print</span>(response.status_code)  <span class="comment"># 属性：响应的状态码</span></span><br><span class="line"><span class="built_in">print</span>(response.text)  <span class="comment"># 属性：获取网站源码</span></span><br><span class="line"><span class="built_in">print</span>(response.headers)  <span class="comment"># 属性：响应的头信息</span></span><br></pre></td></tr></table></figure>
<h3 id="4-2、requests-get请求"><a href="#4-2、requests-get请求" class="headerlink" title="4.2、requests-get请求"></a>4.2、requests-get请求</h3><blockquote>
<p><strong>urllib和requests对比</strong></p>
</blockquote>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># urllib</span></span><br><span class="line"><span class="comment"># (1) 一个类型以及六个方法</span></span><br><span class="line"><span class="comment"># （2）get请求</span></span><br><span class="line"><span class="comment"># （3）post请求   百度翻译</span></span><br><span class="line"><span class="comment"># （4）ajax的get请求</span></span><br><span class="line"><span class="comment"># （5）ajax的post请求</span></span><br><span class="line"><span class="comment"># （6）cookie登陆 微博</span></span><br><span class="line"><span class="comment"># （7）代理</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment"># requests</span></span><br><span class="line"><span class="comment"># (1)一个类型以及六个属性</span></span><br><span class="line"><span class="comment"># （2）get请求</span></span><br><span class="line"><span class="comment"># （3）post请求</span></span><br><span class="line"><span class="comment"># （4）代理</span></span><br><span class="line"><span class="comment"># （5）cookie  验证码</span></span><br></pre></td></tr></table></figure>
<blockquote>
<p><strong>request-get请求使用</strong></p>
</blockquote>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> requests</span><br><span class="line"></span><br><span class="line">url = <span class="string">&#x27;https://www.baidu.com/s&#x27;</span></span><br><span class="line"></span><br><span class="line">headers = &#123;</span><br><span class="line">    <span class="string">&#x27;User-Agent&#x27;</span>: <span class="string">&#x27;Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.159 Safari/537.36&#x27;</span></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">data = &#123;</span><br><span class="line">    <span class="string">&#x27;wd&#x27;</span>: <span class="string">&#x27;北京&#x27;</span></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment"># url  请求资源路径</span></span><br><span class="line"><span class="comment"># params 参数</span></span><br><span class="line"><span class="comment"># kwargs 字典</span></span><br><span class="line">response = requests.get(url=url, params=data, headers=headers)</span><br><span class="line">content = response.text</span><br><span class="line"><span class="built_in">print</span>(content)</span><br><span class="line"></span><br><span class="line"><span class="comment"># 总结：</span></span><br><span class="line"><span class="comment"># （1）参数使用params传递</span></span><br><span class="line"><span class="comment"># （2）参数无需urlencode编码</span></span><br><span class="line"><span class="comment"># （3）不需要请求对象的定制</span></span><br><span class="line"><span class="comment"># （4）请求资源路径中的？可以加也可以不加</span></span><br></pre></td></tr></table></figure>
<h3 id="4-3、requests-post请求"><a href="#4-3、requests-post请求" class="headerlink" title="4.3、requests-post请求"></a>4.3、requests-post请求</h3><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> requests</span><br><span class="line"><span class="keyword">import</span> json</span><br><span class="line"></span><br><span class="line">url = <span class="string">&#x27;https://fanyi.baidu.com/sug&#x27;</span></span><br><span class="line"></span><br><span class="line">headers = &#123;</span><br><span class="line">    <span class="string">&#x27;User-Agent&#x27;</span>: <span class="string">&#x27;Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.159 Safari/537.36&#x27;</span></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">data = &#123;</span><br><span class="line">    <span class="string">&#x27;kw&#x27;</span>: <span class="string">&#x27;eye&#x27;</span></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment"># url 请求地址</span></span><br><span class="line"><span class="comment"># data 请求参数</span></span><br><span class="line"><span class="comment"># kwargs 字典</span></span><br><span class="line">response = requests.post(url=url, data=data, headers=headers)</span><br><span class="line"></span><br><span class="line">content = response.text</span><br><span class="line"></span><br><span class="line">obj = json.loads(content)</span><br><span class="line"><span class="built_in">print</span>(obj)</span><br><span class="line"></span><br><span class="line"><span class="comment"># 总结：</span></span><br><span class="line"><span class="comment"># （1）post请求 是不需要编解码</span></span><br><span class="line"><span class="comment"># （2）post请求的参数是data</span></span><br><span class="line"><span class="comment"># （3）不需要请求对象的定制</span></span><br></pre></td></tr></table></figure>
<h3 id="4-4、代理"><a href="#4-4、代理" class="headerlink" title="4.4、代理"></a>4.4、代理</h3><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> requests</span><br><span class="line"></span><br><span class="line">url = <span class="string">&#x27;https://www.baidu.com/s?&#x27;</span></span><br><span class="line"></span><br><span class="line">headers = &#123;</span><br><span class="line">    <span class="string">&#x27;User-Agent&#x27;</span>: <span class="string">&#x27;Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.159 Safari/537.36&#x27;</span>,</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">data = &#123;</span><br><span class="line">    <span class="string">&#x27;wd&#x27;</span>: <span class="string">&#x27;ip&#x27;</span></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">proxy = &#123;</span><br><span class="line">    <span class="string">&#x27;http&#x27;</span>: <span class="string">&#x27;183.240.203.136:8118&#x27;</span></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">response = requests.get(url=url, params=data, headers=headers, proxies=proxy)</span><br><span class="line"></span><br><span class="line">content = response.text</span><br><span class="line"></span><br><span class="line"><span class="keyword">with</span> <span class="built_in">open</span>(<span class="string">&#x27;代理.html&#x27;</span>, <span class="string">&#x27;w&#x27;</span>, encoding=<span class="string">&#x27;utf-8&#x27;</span>)<span class="keyword">as</span> fp:</span><br><span class="line">    fp.write(content)</span><br></pre></td></tr></table></figure>
<h2 id="5、scrapy"><a href="#5、scrapy" class="headerlink" title="5、scrapy"></a>5、scrapy</h2><h3 id="5-1、scrapy"><a href="#5-1、scrapy" class="headerlink" title="5.1、scrapy"></a>5.1、scrapy</h3><h4 id="5-1-1、scrapy简介"><a href="#5-1-1、scrapy简介" class="headerlink" title="5.1.1、scrapy简介"></a>5.1.1、scrapy简介</h4><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1</span>、scrapy是什么？</span><br><span class="line">	scrapy是为了一个爬取网页数据，提取结构性数据而编写的应用框架，可以应用在包括数据挖掘，信息处理或存储历史数据等一系列的程序中</span><br><span class="line"></span><br><span class="line"><span class="number">2</span>、安装scrapy</span><br><span class="line">	pip install scrapy</span><br><span class="line">   	</span><br><span class="line">    安装出错，自行百度</span><br></pre></td></tr></table></figure>
<h4 id="5-1-2、scrapy项目的创建及运行"><a href="#5-1-2、scrapy项目的创建及运行" class="headerlink" title="5.1.2、scrapy项目的创建及运行"></a>5.1.2、scrapy项目的创建及运行</h4><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1</span>、scrapy创建项目</span><br><span class="line">	终端输入：scrapy startproject 项目名称 【例如：scrapy startproject baidu-project】</span><br><span class="line">    </span><br><span class="line"><span class="number">2</span>、项目组成：</span><br><span class="line">	spiders</span><br><span class="line">    	__init__.py</span><br><span class="line">        自定义爬虫文件.py				----&gt; 自己创建、编写的爬虫文件</span><br><span class="line">    __init__.py</span><br><span class="line">    items.py						 ----&gt;定义数据结构的地方 ，是一个集成scrapy.Item的类</span><br><span class="line">    middlewares.py					 ----&gt;中间件 代理</span><br><span class="line">    pipelines.py					 ----&gt;管道文件，里面只有一个类，用于处理下载的后续处理，默认是<span class="number">300</span>优先级，值越小优先级越高（<span class="number">1</span>-<span class="number">1000</span>）</span><br><span class="line">    settings.py						 ----&gt;配置文件，比如：是否遵守robots协议，User-Agent定义等</span><br><span class="line">    </span><br><span class="line"><span class="number">3</span>、创建爬虫文件</span><br><span class="line">	（<span class="number">1</span>）进入到spiders文件夹</span><br><span class="line">    （<span class="number">2</span>）scrapy genspider 爬虫文件的名字  要爬取网页【例如：scrapy genspider baidu  www.baidu.com】</span><br><span class="line">    </span><br><span class="line"><span class="number">4</span>、爬虫文件的基本组成</span><br><span class="line">	集成scrapy.spider类</span><br><span class="line">    	name=<span class="string">&#x27;baidu&#x27;</span> <span class="comment"># 爬虫的名字，用于运行爬虫的时候使用的值</span></span><br><span class="line">        allowed_domains = [<span class="string">&#x27;www.baidu.com&#x27;</span>]  <span class="comment"># 允许访问的域名，在爬取的时候，如果不是在此域名之下的url，会被过滤掉</span></span><br><span class="line">		start_urls = [<span class="string">&#x27;http://www.baidu.com/&#x27;</span>] <span class="comment"># 声明了爬虫的起始地址。可以写多个url，一般是一个</span></span><br><span class="line">        <span class="keyword">def</span> <span class="title function_">parse</span>(<span class="params">self, response</span>): <span class="comment"># 解析数据的回调函数</span></span><br><span class="line">       		response.txt <span class="comment"># 响应的字符串</span></span><br><span class="line">			response.body <span class="comment"># 响应的是二进制文件</span></span><br><span class="line">            response.xpath() <span class="comment"># xpath方法的返回值类型是selector列表</span></span><br><span class="line">            extract() <span class="comment"># 提取的是selector对象的是data</span></span><br><span class="line">            extract_first() <span class="comment"># 提取的是selector列表中的第一个数据</span></span><br><span class="line">            </span><br><span class="line"><span class="number">5</span>、scrapy文件运行</span><br><span class="line">	scrapy crawl 文件名称【例如：在spiders文件夹中编写的python文件<span class="string">&#x27;get_baidu.py&#x27;</span>，运行：scrapy crawl get_baidu】</span><br><span class="line">    注意：应放在spiders文件夹内部执行</span><br></pre></td></tr></table></figure>
<h4 id="5-1-3、scrapy架构组成"><a href="#5-1-3、scrapy架构组成" class="headerlink" title="5.1.3、scrapy架构组成"></a>5.1.3、scrapy架构组成</h4><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1</span>、引擎						-----&gt;自动运行，无需关注</span><br><span class="line"><span class="number">2</span>、下载器					    ----&gt;从引擎处获取到请求对象后，请求数据</span><br><span class="line"><span class="number">3</span>、spiders					 -----&gt;spider类定义了如何爬取某个（或某些）网站，包括了爬取的动作（例如：是否跟进链接）以及如何从网页的内容中提取结构化数据（爬取item）。换句话说，spider就是定义爬取的动作及分析某个网页（或有些网页）的地方。</span><br><span class="line"><span class="number">4</span>、调度器						----&gt;有自己的调度规则，无需关注</span><br><span class="line"><span class="number">5</span>、管道(Item pipeline)          ----&gt;最终处理数据的管道，会预留接口供我们处理数据。当Item在Spider中被收集之后，它将会被传递到Item pipeline，一些组件会按照一定的执行顺序执行对Item的处理，每个Item pipeline（有时称之为”Item pipeline“）是实现了简单方法的python类，他们接收到Item并通过它执行一些行为，同时也决定此Item是否创建通过pipeline，或是被丢弃而不再进行处理</span><br><span class="line">	以下是item pipeline的一些典型应用：	</span><br><span class="line">    	清理HTML数据</span><br><span class="line">        验证爬取的数据（检查Item包含某些字段）</span><br><span class="line">        查重（并丢弃）</span><br><span class="line">        将爬取结果保存到数据库中</span><br></pre></td></tr></table></figure>
<h4 id="5-1-4、scrapy工作原理"><a href="#5-1-4、scrapy工作原理" class="headerlink" title="5.1.4、scrapy工作原理"></a>5.1.4、scrapy工作原理</h4><p><img src= "" data-lazy-src="https://www.naste.top:9000/webp/image-20210908202558151.webp" alt="image-20210908202558151"></p>
<p><img src= "" data-lazy-src="https://www.naste.top:9000/webp/image-20210908202725951.webp" alt="image-20210908202725951"></p>
<h3 id="5-2、scrapy-shell"><a href="#5-2、scrapy-shell" class="headerlink" title="5.2、scrapy shell"></a>5.2、scrapy shell</h3><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1</span>、什么是scrapy shell?</span><br><span class="line">	scrapy终端，是一个交互终端，可以在未启动spider的情况下尝试及调试爬取代码，其本意是用来测试提取数据的代码。不过，可以将其视作为正常的python终端，在上面测试任何python代码</span><br><span class="line">	该终端是用来测试xpath</span><br><span class="line">	或css表达式，查看他们的工作方式及从爬取的网页中提取的数据。在编写spider时，该终端提供了交互性测试的表达式代码的功能，免去了每次修改后运行spider的麻烦</span><br><span class="line">	</span><br><span class="line"><span class="number">2</span>、安装ipython</span><br><span class="line">	安装：pip install ipython</span><br><span class="line">	简介：如果安装了ipython，scrapy终端将使用ipython（替代标准python终端）。ipython终端与其他相比，更强大，提供了代码自动补全，高亮等其他功能</span><br><span class="line">	</span><br><span class="line"><span class="number">3</span>、应用</span><br><span class="line">	scrapy shell www.baidu.com</span><br><span class="line">	scrapy shell http://www.baidu.com</span><br><span class="line">	scrapy shell <span class="string">&quot;http://www.baidu.com&quot;</span></span><br><span class="line">	scrapy shell <span class="string">&quot;www.baidu.com&quot;</span></span><br><span class="line">    </span><br><span class="line"><span class="number">4</span>、语法</span><br><span class="line">	(<span class="number">1</span>)response对象：</span><br><span class="line">    	response.body、response.text、response.url、response.status</span><br><span class="line">    (<span class="number">2</span>)response的解析：</span><br><span class="line">    	response.xpath() <span class="comment"># 常用</span></span><br><span class="line">        	使用xpath路径查询特定元素，返回一个selector列表对象</span><br><span class="line">        response.css()</span><br><span class="line">        	使用css_selector查询元素，返回一个selector列表对象</span><br><span class="line">            获取内容：response.css(<span class="string">&#x27;#su::text&#x27;</span>).extract_first()</span><br><span class="line">            获取属性：response.css(<span class="string">&#x27;#su::attr(&quot;value&quot;)&#x27;</span>).extrace_first()</span><br><span class="line">        extract_first()</span><br><span class="line">        	提取selector列表中的第一个值</span><br><span class="line">            如果提取不到值，会返回一个空值</span><br><span class="line">            返回第一个解析到的值，如果列表为空，此方法不会报错，会返回一个空值</span><br><span class="line">        xpath()</span><br><span class="line">        css()</span><br><span class="line">        	注意：每一个selector对象可以再次的去使用xpath或css方法</span><br></pre></td></tr></table></figure>
<h3 id="5-3、yield"><a href="#5-3、yield" class="headerlink" title="5.3、yield"></a>5.3、yield</h3><ul>
<li>带有yield的函数不再是一个普通函数，而是一个生成器generator，可用于迭代</li>
<li>yield是一个类似return的关键字，迭代一次遇到yield时就返回yield后面（右边）的值。重点是：下一次迭代时，从上一次迭代遇到的yield后面的代码（下一行）开始执行</li>
<li>简单理解：yield就是return返回一个值，并且记住这个返回值的位置，下次迭代就是从这个位置后（下一次）开始</li>
</ul>
<p>案例：</p>
<ul>
<li>当当网：(1)yield     (2) 管道封装    (3)多条管道下载    (4) 多页数据下载</li>
<li>电影天堂   (1) 一个item包含多级页面的数据</li>
</ul>
<h4 id="5-4、"><a href="#5-4、" class="headerlink" title="5.4、"></a>5.4、</h4><h3 id="5-5、MySQL和pymysql安装和使用"><a href="#5-5、MySQL和pymysql安装和使用" class="headerlink" title="5.5、MySQL和pymysql安装和使用"></a>5.5、MySQL和pymysql安装和使用</h3><blockquote>
<p><strong>MySQL安装</strong></p>
</blockquote>
<p><strong>自行百度查询</strong></p>
<blockquote>
<p><strong>pymysql安装和使用</strong></p>
</blockquote>
<p><strong>pymysql是一个连接mysql的工具</strong></p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 安装</span></span><br><span class="line">pip install pymysql</span><br><span class="line"></span><br><span class="line"><span class="comment"># 连接使用</span></span><br><span class="line">conn = pymysql.connect(host,port,user,password,db,charset)</span><br><span class="line">cursor = conn.cursor()</span><br><span class="line">cursor.executer()</span><br></pre></td></tr></table></figure>
<h3 id="5-4、当当网"><a href="#5-4、当当网" class="headerlink" title="5.4、当当网"></a>5.4、当当网</h3><h3 id="5-5、电影天堂"><a href="#5-5、电影天堂" class="headerlink" title="5.5、电影天堂"></a>5.5、电影天堂</h3></article><div class="post-copyright"><div class="post-copyright__title"><span class="post-copyright-info"><h>Python爬虫基本使用</h></span></div><div class="post-copyright__type"><span class="post-copyright-info"><a href="https://www.naste.top:1024/posts/ad44f2a3.html">https://www.naste.top:1024/posts/ad44f2a3.html</a></span></div><div class="post-copyright-m"><div class="post-copyright-m-info"><div class="post-copyright-a"><h>作者</h><div class="post-copyright-cc-info"><h>XiaoFei🥝</h></div></div><div class="post-copyright-c"><h>发布于</h><div class="post-copyright-cc-info"><h>2022-06-22</h></div></div><div class="post-copyright-u"><h>更新于</h><div class="post-copyright-cc-info"><h>2024-04-26</h></div></div><div class="post-copyright-c"><h>许可协议</h><div class="post-copyright-cc-info"><a class="icon" rel="noopener" target="_blank" title="Creative Commons" href="https://creativecommons.org/"><i class="fab fa-creative-commons"></i></a><a rel="noopener" target="_blank" title="CC BY-NC-SA 4.0" href="https://creativecommons.org/licenses/by-nc-sa/4.0/">CC BY-NC-SA 4.0</a></div></div></div></div></div><div class="tag_share"><div class="post-meta__tag-list"><a class="post-meta__tags" href="/tags/Python/"><div class="tags-punctuation"><svg class="faa-tada icon" style="height:1.1em;width:1.1em;fill:currentColor;position:relative;top:2px;margin-right:3px" aria-hidden="true"><use xlink:href="#icon-sekuaibiaoqian"></use></svg></div>Python</a><a class="post-meta__tags" href="/tags/urllib/"><div class="tags-punctuation"><svg class="faa-tada icon" style="height:1.1em;width:1.1em;fill:currentColor;position:relative;top:2px;margin-right:3px" aria-hidden="true"><use xlink:href="#icon-sekuaibiaoqian"></use></svg></div>urllib</a><a class="post-meta__tags" href="/tags/Selenium/"><div class="tags-punctuation"><svg class="faa-tada icon" style="height:1.1em;width:1.1em;fill:currentColor;position:relative;top:2px;margin-right:3px" aria-hidden="true"><use xlink:href="#icon-sekuaibiaoqian"></use></svg></div>Selenium</a><a class="post-meta__tags" href="/tags/requests/"><div class="tags-punctuation"><svg class="faa-tada icon" style="height:1.1em;width:1.1em;fill:currentColor;position:relative;top:2px;margin-right:3px" aria-hidden="true"><use xlink:href="#icon-sekuaibiaoqian"></use></svg></div>requests</a><a class="post-meta__tags" href="/tags/scrapy/"><div class="tags-punctuation"><svg class="faa-tada icon" style="height:1.1em;width:1.1em;fill:currentColor;position:relative;top:2px;margin-right:3px" aria-hidden="true"><use xlink:href="#icon-sekuaibiaoqian"></use></svg></div>scrapy</a></div></div><link rel="stylesheet" href="/css/coin.css" media="defer" onload="this.media='all'"/><div class="post-reward"><button class="tip-button reward-button"><span class="tip-button__text">投喂作者</span><div class="coin-wrapper"><div class="coin"><div class="coin__middle"></div><div class="coin__back"></div><div class="coin__front"></div></div></div><div class="reward-main"><ul class="reward-all"><li class="reward-item"><a href="https://www.naste.top:9000/images/wechat.jpg" target="_blank"><img class="post-qr-code-img" src= "" data-lazy-src="https://www.naste.top:9000/images/wechat.jpg" alt="微信"/></a><div class="post-qr-code-desc">微信</div></li><li class="reward-item"><a href="https://www.naste.top:9000/images/alipay.jpg" target="_blank"><img class="post-qr-code-img" src= "" data-lazy-src="https://www.naste.top:9000/images/alipay.jpg" alt="支付宝"/></a><div class="post-qr-code-desc">支付宝</div></li></ul></div></button></div><audio id="coinAudio" src="https://npm.elemecdn.com/akilar-candyassets@1.0.36/audio/aowu.m4a"></audio><script defer="defer" src="/js/coin.js"></script><nav class="pagination-post" id="pagination"><div class="prev-post pull-left"><a href="/posts/4c0ac24d.html"><img class="prev-cover" src= "" data-lazy-src="https://www.naste.top:9000/webp/1515952743787.webp" onerror="onerror=null;src='/assets/r2.jpg'" alt="cover of previous post"><div class="pagination-info"><div class="label">上一篇</div><div class="prev_info">Vue开发技巧</div></div></a></div><div class="next-post pull-right"><a href="/posts/99832f47.html"><img class="next-cover" src= "" data-lazy-src="https://www.naste.top:9000/webp/2514ce637e374a318653a40133bc66c5.webp" onerror="onerror=null;src='/assets/r2.jpg'" alt="cover of next post"><div class="pagination-info"><div class="label">下一篇</div><div class="next_info">Python基础</div></div></a></div></nav><div class="relatedPosts"><div class="headline"><i class="fas fa-thumbs-up fa-fw"></i><span>相关推荐</span></div><div class="relatedPosts-list"><div><a href="/posts/99832f47.html" title="Python基础"><img class="cover" src= "" data-lazy-src="https://www.naste.top:9000/webp/2514ce637e374a318653a40133bc66c5.webp" alt="cover"><div class="content is-center"><div class="date"><i class="fas fa-history fa-fw"></i> 2024-04-26</div><div class="title">Python基础</div></div></a></div></div></div><hr/><div id="post-comment"><div class="comment-head"><div class="comment-headline"><i class="fas fa-comments fa-fw"></i><span> 评论</span></div></div><div class="comment-wrap"><div><div id="twikoo-wrap"></div></div></div></div></div><div class="aside-content" id="aside-content"><div class="sticky_layout"><div class="card-widget" id="card-toc"><div class="item-headline"><svg class="meta_icon" style="width:22px;height:22px;position:relative;top:5px"><use xlink:href="#icon-mulu1"></use></svg><span style="font-weight:bold">目录</span><span class="toc-percentage"></span></div><div class="toc-content"><ol class="toc"><li class="toc-item toc-level-1"><a class="toc-link" href="#%E4%B8%80%E3%80%81python%E5%9F%BA%E7%A1%80"><span class="toc-text">一、python基础</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#1%E3%80%81%E6%B3%A8%E9%87%8A"><span class="toc-text">1、注释</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#2%E3%80%81%E5%8F%98%E9%87%8F%E4%BB%A5%E5%8F%8A%E6%95%B0%E6%8D%AE%E7%B1%BB%E5%9E%8B"><span class="toc-text">2、变量以及数据类型</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#2-1%E3%80%81%E5%8F%98%E9%87%8F%E5%AE%9A%E4%B9%89"><span class="toc-text">2.1、变量定义</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#2-2%E3%80%81%E6%9F%A5%E7%9C%8B%E5%8F%98%E9%87%8F%E7%9A%84%E6%95%B0%E6%8D%AE%E7%B1%BB%E5%9E%8B"><span class="toc-text">2.2、查看变量的数据类型</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#3%E3%80%81%E7%B1%BB%E5%9E%8B%E8%BD%AC%E6%8D%A2"><span class="toc-text">3、类型转换</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#4%E3%80%81%E8%BF%90%E7%AE%97%E7%AC%A6"><span class="toc-text">4、运算符</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#4-1%E3%80%81%E7%AE%97%E6%95%B0%E8%BF%90%E7%AE%97%E7%AC%A6"><span class="toc-text">4.1、算数运算符</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#4-2%E3%80%81%E8%B5%8B%E5%80%BC%E8%BF%90%E7%AE%97%E7%AC%A6"><span class="toc-text">4.2、赋值运算符</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#4-3%E3%80%81%E5%A4%8D%E5%90%88%E8%B5%8B%E5%80%BC%E8%BF%90%E7%AE%97%E7%AC%A6"><span class="toc-text">4.3、复合赋值运算符</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#4-4%E3%80%81%E6%AF%94%E8%BE%83%E8%BF%90%E7%AE%97%E7%AC%A6"><span class="toc-text">4.4、比较运算符</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#4-5%E3%80%81%E9%80%BB%E8%BE%91%E8%BF%90%E7%AE%97%E7%AC%A6"><span class="toc-text">4.5、逻辑运算符</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#5%E3%80%81%E8%BE%93%E5%85%A5%E5%92%8C%E8%BE%93%E5%87%BA"><span class="toc-text">5、输入和输出</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#5-1%E3%80%81%E8%BE%93%E5%87%BA"><span class="toc-text">5.1、输出</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#5-2%E3%80%81%E8%BE%93%E5%85%A5"><span class="toc-text">5.2、输入</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#6%E3%80%81%E6%B5%81%E7%A8%8B%E6%8E%A7%E5%88%B6%E8%AF%AD%E5%8F%A5"><span class="toc-text">6、流程控制语句</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#6-1%E3%80%81if"><span class="toc-text">6.1、if</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#6-2%E3%80%81if-else"><span class="toc-text">6.2、if-else</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#6-3%E3%80%81elif"><span class="toc-text">6.3、elif</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#6-4%E3%80%81for"><span class="toc-text">6.4、for</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#6-5%E3%80%81range"><span class="toc-text">6.5、range</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#7%E3%80%81%E6%95%B0%E6%8D%AE%E7%B1%BB%E5%9E%8B%E9%AB%98%E7%BA%A7"><span class="toc-text">7、数据类型高级</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#7-1%E3%80%81%E5%AD%97%E7%AC%A6%E4%B8%B2%E9%AB%98%E7%BA%A7"><span class="toc-text">7.1、字符串高级</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#7-2%E3%80%81%E5%88%97%E8%A1%A8%E9%AB%98%E7%BA%A7"><span class="toc-text">7.2、列表高级</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#7-2-1%E3%80%81%E5%88%97%E8%A1%A8%E6%B7%BB%E5%8A%A0"><span class="toc-text">7.2.1、列表添加</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#7-2-2%E3%80%81%E5%88%97%E8%A1%A8%E4%BF%AE%E6%94%B9"><span class="toc-text">7.2.2、列表修改</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#7-2-3%E3%80%81%E5%88%97%E8%A1%A8%E6%9F%A5%E8%AF%A2"><span class="toc-text">7.2.3、列表查询</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#7-2-4%E3%80%81%E5%88%97%E8%A1%A8%E5%88%A0%E9%99%A4"><span class="toc-text">7.2.4、列表删除</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#7-3%E3%80%81%E5%85%83%E7%BB%84%E9%AB%98%E7%BA%A7"><span class="toc-text">7.3、元组高级</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#7-3-1%E3%80%81%E5%85%83%E7%BB%84%E6%9F%A5%E8%AF%A2"><span class="toc-text">7.3.1、元组查询</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#7-4%E3%80%81%E5%88%87%E7%89%87"><span class="toc-text">7.4、切片</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#7-5%E3%80%81%E5%AD%97%E5%85%B8%E9%AB%98%E7%BA%A7"><span class="toc-text">7.5、字典高级</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#7-5-1%E3%80%81%E5%AD%97%E5%85%B8%E6%9F%A5%E8%AF%A2"><span class="toc-text">7.5.1、字典查询</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#7-5-2%E3%80%81%E5%AD%97%E5%85%B8%E4%BF%AE%E6%94%B9"><span class="toc-text">7.5.2、字典修改</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#7-5-3%E3%80%81%E5%AD%97%E5%85%B8%E6%B7%BB%E5%8A%A0"><span class="toc-text">7.5.3、字典添加</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#7-5-4%E3%80%81%E5%AD%97%E5%85%B8%E5%88%A0%E9%99%A4"><span class="toc-text">7.5.4、字典删除</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#7-5-5%E3%80%81%E5%AD%97%E5%85%B8%E9%81%8D%E5%8E%86"><span class="toc-text">7.5.5、字典遍历</span></a></li></ol></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#8%E3%80%81%E5%87%BD%E6%95%B0"><span class="toc-text">8、函数</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#8-1%E3%80%81%E5%87%BD%E6%95%B0%E5%AE%9A%E4%B9%89"><span class="toc-text">8.1、函数定义</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#8-2%E3%80%81%E5%87%BD%E6%95%B0%E8%B0%83%E7%94%A8"><span class="toc-text">8.2、函数调用</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#8-3%E3%80%81%E5%87%BD%E6%95%B0%E5%8F%82%E6%95%B0"><span class="toc-text">8.3、函数参数</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#8-4%E3%80%81%E5%87%BD%E6%95%B0%E8%BF%94%E5%9B%9E%E5%80%BC"><span class="toc-text">8.4、函数返回值</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#8-5%E3%80%81%E5%B1%80%E9%83%A8%E5%8F%98%E9%87%8F"><span class="toc-text">8.5、局部变量</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#8-6%E3%80%81%E5%85%A8%E5%B1%80%E5%8F%98%E9%87%8F"><span class="toc-text">8.6、全局变量</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#10%E3%80%81%E6%96%87%E4%BB%B6"><span class="toc-text">10、文件</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#10-1%E3%80%81%E6%96%87%E4%BB%B6%E7%9A%84%E6%89%93%E5%BC%80%E4%B8%8E%E5%85%B3%E9%97%AD"><span class="toc-text">10.1、文件的打开与关闭</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#10-1-1%E3%80%81%E6%89%93%E5%BC%80%E6%96%87%E4%BB%B6-%E5%88%9B%E5%BB%BA%E6%96%87%E4%BB%B6"><span class="toc-text">10.1.1、打开文件&#x2F;创建文件</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#10-2%E3%80%81%E6%96%87%E4%BB%B6%E7%9A%84%E8%AF%BB%E5%86%99"><span class="toc-text">10.2、文件的读写</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#10-3%E3%80%81%E5%BA%8F%E5%88%97%E5%8C%96%E5%92%8C%E5%8F%8D%E5%BA%8F%E5%88%97%E5%8C%96"><span class="toc-text">10.3、序列化和反序列化</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#11%E3%80%81%E5%BC%82%E5%B8%B8"><span class="toc-text">11、异常</span></a></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#%E4%BA%8C%E3%80%81%E7%88%AC%E8%99%AB%E6%8A%80%E6%9C%AF"><span class="toc-text">二、爬虫技术</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#1%E3%80%81urllib"><span class="toc-text">1、urllib</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#1-1%E3%80%81urllib%E5%BA%93%E4%BD%BF%E7%94%A8"><span class="toc-text">1.1、urllib库使用</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#1-2%E3%80%81%E8%AF%B7%E6%B1%82%E5%AF%B9%E8%B1%A1%E5%AE%9A%E5%88%B6"><span class="toc-text">1.2、请求对象定制</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#1-3%E3%80%81%E7%BC%96%E8%A7%A3%E7%A0%81"><span class="toc-text">1.3、编解码</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#1-3-1%E3%80%81Get%E8%AF%B7%E6%B1%82%E5%8F%82%E6%95%B0%E7%BC%96%E7%A0%81"><span class="toc-text">1.3.1、Get请求参数编码</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#1-3-2%E3%80%81Post%E8%AF%B7%E6%B1%82"><span class="toc-text">1.3.2、Post请求</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#1-3-3%E3%80%81get%E8%AF%B7%E6%B1%82%E5%92%8Cpost%E8%AF%B7%E6%B1%82%E7%9A%84%E5%8C%BA%E5%88%AB"><span class="toc-text">1.3.3、get请求和post请求的区别</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#1-4%E3%80%81Ajax%E7%9A%84get%E8%AF%B7%E6%B1%82"><span class="toc-text">1.4、Ajax的get请求</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#1-5%E3%80%81Ajax%E7%9A%84post%E8%AF%B7%E6%B1%82"><span class="toc-text">1.5、Ajax的post请求</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#1-6%E3%80%81%E7%88%AC%E8%99%AB%E5%BC%82%E5%B8%B8"><span class="toc-text">1.6、爬虫异常</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#1-7%E3%80%81cookie%E7%99%BB%E5%BD%95"><span class="toc-text">1.7、cookie登录</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#1-8%E3%80%81Handler%E5%A4%84%E7%90%86%E5%99%A8"><span class="toc-text">1.8、Handler处理器</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#1-9%E3%80%81%E4%BB%A3%E7%90%86%E6%9C%8D%E5%8A%A1%E5%99%A8"><span class="toc-text">1.9、代理服务器</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#1-9-1%E3%80%81%E5%8D%95%E4%B8%AA%E4%BB%A3%E7%90%86"><span class="toc-text">1.9.1、单个代理</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#1-9-2%E3%80%81%E4%BB%A3%E7%90%86%E6%B1%A0"><span class="toc-text">1.9.2、代理池</span></a></li></ol></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#2%E3%80%81%E8%A7%A3%E6%9E%90"><span class="toc-text">2、解析</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#2-1%E3%80%81xpath"><span class="toc-text">2.1、xpath</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#2-1-1%E3%80%81xpath%E5%AE%89%E8%A3%85"><span class="toc-text">2.1.1、xpath安装</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#2-1-2%E3%80%81xpath%E5%9F%BA%E6%9C%AC%E8%AF%AD%E6%B3%95"><span class="toc-text">2.1.2、xpath基本语法</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#2-1-3%E3%80%81xpath%E5%9F%BA%E6%9C%AC%E4%BD%BF%E7%94%A8"><span class="toc-text">2.1.3、xpath基本使用</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#2-2%E3%80%81jsonpath"><span class="toc-text">2.2、jsonpath</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#2-3%E3%80%81-BeautifulSoup"><span class="toc-text">2.3、.BeautifulSoup</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#2-3-1%E3%80%81%E5%AE%89%E8%A3%85%E5%8F%8A%E7%AE%80%E4%BB%8B"><span class="toc-text">2.3.1、安装及简介</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#2-3-2%E3%80%81bs4%E7%9A%84%E5%9F%BA%E6%9C%AC%E4%BD%BF%E7%94%A8"><span class="toc-text">2.3.2、bs4的基本使用</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#2-3-3%E3%80%81%E8%8A%82%E7%82%B9%E4%BF%A1%E6%81%AF-%E8%8E%B7%E5%8F%96%E6%A0%87%E7%AD%BE%E5%86%85%E5%AE%B9"><span class="toc-text">2.3.3、节点信息-获取标签内容</span></a></li></ol></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#3%E3%80%81Selenium"><span class="toc-text">3、Selenium</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#3-1%E3%80%81selenium"><span class="toc-text">3.1、selenium</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#3-1-1%E3%80%81Seleneum%E5%9F%BA%E6%9C%AC%E4%BD%BF%E7%94%A8"><span class="toc-text">3.1.1、Seleneum基本使用</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#3-1-2%E3%80%81Selenium%E5%85%83%E7%B4%A0%E5%AE%9A%E4%BD%8D"><span class="toc-text">3.1.2、Selenium元素定位</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#3-1-3%E3%80%81selenium%E8%AE%BF%E9%97%AE%E5%85%83%E7%B4%A0%E4%BF%A1%E6%81%AF"><span class="toc-text">3.1.3、selenium访问元素信息</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#3-1-4%E3%80%81selenium%E4%BA%A4%E4%BA%92"><span class="toc-text">3.1.4、selenium交互</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#3-2%E3%80%81Phantomjs"><span class="toc-text">3.2、Phantomjs</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#3-3%E3%80%81Chrome-handless"><span class="toc-text">3.3、Chrome handless</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#4%E3%80%81requests"><span class="toc-text">4、requests</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#4-1%E3%80%81%E5%9F%BA%E6%9C%AC%E4%BD%BF%E7%94%A8"><span class="toc-text">4.1、基本使用</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#4-2%E3%80%81requests-get%E8%AF%B7%E6%B1%82"><span class="toc-text">4.2、requests-get请求</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#4-3%E3%80%81requests-post%E8%AF%B7%E6%B1%82"><span class="toc-text">4.3、requests-post请求</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#4-4%E3%80%81%E4%BB%A3%E7%90%86"><span class="toc-text">4.4、代理</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#5%E3%80%81scrapy"><span class="toc-text">5、scrapy</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#5-1%E3%80%81scrapy"><span class="toc-text">5.1、scrapy</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#5-1-1%E3%80%81scrapy%E7%AE%80%E4%BB%8B"><span class="toc-text">5.1.1、scrapy简介</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#5-1-2%E3%80%81scrapy%E9%A1%B9%E7%9B%AE%E7%9A%84%E5%88%9B%E5%BB%BA%E5%8F%8A%E8%BF%90%E8%A1%8C"><span class="toc-text">5.1.2、scrapy项目的创建及运行</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#5-1-3%E3%80%81scrapy%E6%9E%B6%E6%9E%84%E7%BB%84%E6%88%90"><span class="toc-text">5.1.3、scrapy架构组成</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#5-1-4%E3%80%81scrapy%E5%B7%A5%E4%BD%9C%E5%8E%9F%E7%90%86"><span class="toc-text">5.1.4、scrapy工作原理</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#5-2%E3%80%81scrapy-shell"><span class="toc-text">5.2、scrapy shell</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#5-3%E3%80%81yield"><span class="toc-text">5.3、yield</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#5-4%E3%80%81"><span class="toc-text">5.4、</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#5-5%E3%80%81MySQL%E5%92%8Cpymysql%E5%AE%89%E8%A3%85%E5%92%8C%E4%BD%BF%E7%94%A8"><span class="toc-text">5.5、MySQL和pymysql安装和使用</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#5-4%E3%80%81%E5%BD%93%E5%BD%93%E7%BD%91"><span class="toc-text">5.4、当当网</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#5-5%E3%80%81%E7%94%B5%E5%BD%B1%E5%A4%A9%E5%A0%82"><span class="toc-text">5.5、电影天堂</span></a></li></ol></li></ol></li></ol></div></div></div></div></main><footer id="footer" style="background-color: transparent;"><div id="footer-wrap"><div id="ft"><div class="ft-item-1"><div class="t-top"><div class="t-t-l"><p class="ft-t t-l-t">格言🧬</p><div class="bg-ad"><div>再看看那个光点，它就在这里，这是家园，这是我们 —— 你所爱的每一个人，你认识的一个人，你听说过的每一个人，曾经有过的每一个人，都在它上面度过他们的一生✨</div><div class="btn-xz-box"><a class="btn-xz" target="_blank" rel="noopener" href="https://stellarium.org/">点击开启星辰之旅</a></div></div></div><div class="t-t-r"><p class="ft-t t-l-t">猜你想看💡</p><ul class="ft-links"><li><a href="/posts/eec9786.html">魔改指南</a><a href="/box/nav/">网址导航</a></li><li><a href="/social/link/">我的朋友</a><a href="/comments/">留点什么</a></li><li><a href="/personal/about/">关于作者</a><a href="/archives/">文章归档</a></li><li><a href="/categories/">文章分类</a><a href="/tags/">文章标签</a></li><li><a href="/box/Gallery/">我的画廊</a><a href="/personal/bb/">我的唠叨</a></li><li><a href="/site/time/">建设进程</a><a href="/site/census/">网站统计</a></li></ul></div></div></div><div class="ft-item-2"><p class="ft-t">推荐友链⌛</p><div class="ft-img-group"><div class="img-group-item"><a href="https://www.naste.top:1024/" title="XiaoFei🥝"><img src= "" data-lazy-src="https://www.naste.top:9000/images/cat.jpg" alt=""/></a></div><div class="img-group-item"><a href="javascript:void(0)" title="广告位招租"><img src= "" data-lazy-src="https://www.naste.top:9000/webp/65307a5828af6790.webp" alt=""/></a></div><div class="img-group-item"><a href="javascript:void(0)" title="广告位招租"><img src= "" data-lazy-src="https://www.naste.top:9000/webp/65307a5828af6790.webp" alt=""/></a></div><div class="img-group-item"><a href="javascript:void(0)" title="广告位招租"><img src= "" data-lazy-src="https://www.naste.top:9000/webp/65307a5828af6790.webp" alt=""/></a></div><div class="img-group-item"><a href="javascript:void(0)" title="广告位招租"><img src= "" data-lazy-src="https://www.naste.top:9000/webp/65307a5828af6790.webp" alt=""/></a></div><div class="img-group-item"><a href="javascript:void(0)" title="广告位招租"><img src= "" data-lazy-src="https://www.naste.top:9000/webp/65307a5828af6790.webp" alt=""/></a></div><div class="img-group-item"><a href="javascript:void(0)" title="广告位招租"><img src= "" data-lazy-src="https://www.naste.top:9000/webp/65307a5828af6790.webp" alt=""/></a></div><div class="img-group-item"><a href="javascript:void(0)" title="广告位招租"><img src= "" data-lazy-src="https://www.naste.top:9000/webp/65307a5828af6790.webp" alt=""/></a></div></div></div></div><div class="copyright"><span><b>&copy;2022-2024</b></span><span><b>&nbsp;&nbsp;By XiaoFei🥝</b></span></div><div id="workboard"></div><p id="ghbdages"><a class="github-badge" target="_blank" href="https://hexo.io/" style="margin-inline:5px" title="博客框架为Hexo_v6.3.0"><img src= "" data-lazy-src="https://static.naste.top:1024/svg/blog/Frame-Hexo-blue.svg" alt=""/></a><a class="github-badge" target="_blank" href="https://butterfly.js.org/" style="margin-inline:5px" title="主题版本Butterfly_v4.3.1"><img src= "" data-lazy-src="https://static.naste.top:1024/svg/blog/Theme-Butterfly-6513df.svg" alt=""/></a><a class="github-badge" target="_blank" href="https://vercel.com/" style="margin-inline:5px" title="本站采用多线部署，主线路托管于Vercel"><img src= "" data-lazy-src="https://static.naste.top:1024/svg/blog/Hosted-Vercel-brightgreen.svg" alt=""/></a><a class="github-badge" target="_blank" href="https://user.51.la/" style="margin-inline:5px" title="本站数据分析得益于51la技术支持"><img src= "" data-lazy-src="https://static.naste.top:1024/svg/blog/Analytics-51la-3db1eb.svg" alt=""/></a><a class="github-badge" target="_blank" href="https://github.com/" style="margin-inline:5px" title="本网站源码由Github提供存储仓库"><img src= "" data-lazy-src=" https://static.naste.top:1024/svg/blog/Source-Github-d021d6.svg" alt=""/></a></p></div></footer></div><div id="rightside"><div id="rightside-config-hide"><button id="readmode" type="button" title="阅读模式"><i class="fas fa-book-open"></i></button><a class="icon-V hidden" onclick="switchNightMode()" title="浅色和深色模式转换"><svg width="25" height="25" viewBox="0 0 1024 1024"><use id="modeicon" xlink:href="#icon-moon"></use></svg></a><button id="hide-aside-btn" type="button" title="单栏和双栏切换"><i class="fas fa-arrows-alt-h"></i></button><button class="share" type="button" title="右键模式" onclick="changeMouseMode()"><i class="fas fa-mouse"></i></button></div><div id="rightside-config-show"><button id="rightside_config" type="button" title="设置"><i class="fas fa-cog right_side"></i></button><button class="close" id="mobile-toc-button" type="button" title="目录"><i class="fas fa-list-ul"></i></button><button class="share" type="button" title="分享链接" onclick="share()"><i class="fas fa-share-nodes"></i></button><a id="to_comment" href="#post-comment" title="直达评论"><i class="fas fa-comments"></i></a><button id="go-up" type="button" title="回到顶部"><i class="fas fa-arrow-up"></i><span id="percent">0<span>%</span></span></button><button id="go-down" type="button" title="直达底部" onclick="btf.scrollToDest(document.body.scrollHeight, 500)"><i class="fas fa-arrow-down"></i></button></div></div><div id="local-search"><div class="search-dialog"><nav class="search-nav"><span class="search-dialog-title">搜索</span><span id="loading-status"></span><button class="search-close-button"><i class="fas fa-times"></i></button></nav><div class="is-center" id="loading-database"><i class="fas fa-spinner fa-pulse"></i><span>  数据库加载中</span></div><div class="search-wrap"><div id="local-search-input"><div class="local-search-box"><input class="local-search-box--input" placeholder="搜索文章" type="text"/></div></div><hr/><div id="local-search-results"></div></div></div><div id="search-mask"></div></div><div class="js-pjax" id="rightMenu"><div class="rightMenu-group rightMenu-small"><a class="rightMenu-item" href="javascript:window.history.back();"><i class="fa fa-arrow-left"></i></a><a class="rightMenu-item" href="javascript:window.history.forward();"><i class="fa fa-arrow-right"></i></a><a class="rightMenu-item" href="javascript:window.location.reload();"><i class="fa fa-refresh"></i></a><a class="rightMenu-item" href="javascript:rmf.scrollToTop();"><i class="fa fa-arrow-up"></i></a></div><div class="rightMenu-group rightMenu-line hide" id="menu-text"><a class="rightMenu-item" href="javascript:rmf.copySelect();"><i class="fa fa-copy"></i><span>复制</span></a><a class="rightMenu-item" href="javascript:window.open(&quot;https://www.baidu.com/s?wd=&quot;+window.getSelection().toString());window.location.reload();"><i class="fa fa-search"></i><span>百度搜索</span></a></div><div class="rightMenu-group rightMenu-line hide" id="menu-too"><a class="rightMenu-item" href="javascript:window.open(window.getSelection().toString());window.location.reload();"><i class="fa fa-link"></i><span>转到链接</span></a></div><div class="rightMenu-group rightMenu-line hide" id="menu-paste"><a class="rightMenu-item" href="javascript:rmf.paste()"><i class="fa fa-copy"></i><span>粘贴</span></a></div><div class="rightMenu-group rightMenu-line hide" id="menu-post"><a class="rightMenu-item" href="#post-comment"><i class="fas fa-comment"></i><span>空降评论</span></a><a class="rightMenu-item" href="javascript:rmf.copyWordsLink()"><i class="fa fa-link"></i><span>复制本文地址</span></a></div><div class="rightMenu-group rightMenu-line hide" id="menu-to"><a class="rightMenu-item" href="javascript:rmf.openWithNewTab()"><i class="fa fa-window-restore"></i><span>新窗口打开</span></a><a class="rightMenu-item" id="menu-too" href="javascript:rmf.open()"><i class="fa fa-link"></i><span>转到链接</span></a><a class="rightMenu-item" href="javascript:rmf.copyLink()"><i class="fa fa-copy"></i><span>复制链接</span></a></div><div class="rightMenu-group rightMenu-line hide" id="menu-img"><a class="rightMenu-item" href="javascript:rmf.saveAs()"><i class="fa fa-download"></i><span>保存图片</span></a><a class="rightMenu-item" href="javascript:rmf.openWithNewTab()"><i class="fa fa-window-restore"></i><span>在新窗口打开</span></a><a class="rightMenu-item" href="javascript:rmf.copyLink()"><i class="fa fa-copy"></i><span>复制图片链接</span></a></div><div class="rightMenu-group rightMenu-line"><a class="rightMenu-item" href="javascript:randomPost()"><i class="fa fa-paper-plane"></i><span>随便逛逛</span></a><a class="rightMenu-item" href="javascript:switchNightMode();"><i class="fa fa-moon"></i><span>昼夜切换</span></a><a class="rightMenu-item" href="/personal/about/"><i class="fa fa-info-circle"></i><span>关于博客</span></a><a class="rightMenu-item" href="javascript:toggleWinbox();"><i class="fas fa-cog"></i><span>美化设置</span></a><a class="rightMenu-item" href="javascript:rmf.fullScreen();"><i class="fas fa-expand"></i><span>切换全屏</span></a><a class="rightMenu-item" href="javascript:window.print();"><i class="fa-solid fa-print"></i><span>打印页面</span></a></div></div><div><script src="/js/utils.js"></script><script src="/js/main.js"></script><script src="https://cdn.staticfile.org/fancyapps-ui/4.0.31/fancybox.umd.min.js"></script><script src="https://lf3-cdn-tos.bytecdntp.com/cdn/expire-1-M/instant.page/5.1.0/instantpage.min.js" type="module"></script><script src="https://lf3-cdn-tos.bytecdntp.com/cdn/expire-1-M/vanilla-lazyload/17.3.1/lazyload.iife.min.js"></script><script>function panguFn () {
  if (typeof pangu === 'object') pangu.autoSpacingPage()
  else {
    getScript('https://cdnjs.cloudflare.com/ajax/libs/pangu/4.0.7/pangu.min.js')
      .then(() => {
        pangu.autoSpacingPage()
      })
  }
}

function panguInit () {
  if (true){
    GLOBAL_CONFIG_SITE.isPost && panguFn()
  } else {
    panguFn()
  }
}

document.addEventListener('DOMContentLoaded', panguInit)</script><script src="/js/search/local-search.js"></script><script async="async">var preloader = {
  endLoading: () => {
    document.body.style.overflow = 'auto';
    document.getElementById('loading-box').classList.add("loaded")
  },
  initLoading: () => {
    document.body.style.overflow = '';
    document.getElementById('loading-box').classList.remove("loaded")

  }
}
window.addEventListener('load',preloader.endLoading())
setTimeout(function(){preloader.endLoading();}, 5000);
document.getElementById('loading-box').addEventListener('click',()=> {preloader.endLoading()})</script><div class="js-pjax"><script>(()=>{
  const init = () => {
    twikoo.init(Object.assign({
      el: '#twikoo-wrap',
      envId: '',
      region: '',
      onCommentLoaded: function () {
        btf.loadLightbox(document.querySelectorAll('#twikoo .tk-content img:not(.tk-owo-emotion)'))
      }
    }, null))
  }

  const getCount = () => {
    const countELement = document.getElementById('twikoo-count')
    if(!countELement) return
    twikoo.getCommentsCount({
      envId: '',
      region: '',
      urls: [window.location.pathname],
      includeReply: false
    }).then(function (res) {
      countELement.innerText = res[0].count
    }).catch(function (err) {
      console.error(err);
    });
  }

  const runFn = () => {
    init()
    
  }

  const loadTwikoo = () => {
    if (typeof twikoo === 'object') {
      setTimeout(runFn,0)
      return
    } 
    getScript('https://cdn.staticfile.org/twikoo/1.6.8/twikoo.all.min.js').then(runFn)
  }

  if ('Twikoo' === 'Twikoo' || !true) {
    if (true) btf.loadComment(document.getElementById('twikoo-wrap'), loadTwikoo)
    else loadTwikoo()
  } else {
    window.loadOtherComment = () => {
      loadTwikoo()
    }
  }
})()</script></div><script src="https://cdn.staticfile.org/jquery/3.6.3/jquery.min.js"></script><script async src="https://cdn1.tianli0.top/npm/vue@2.6.14/dist/vue.min.js"></script><script async src="https://cdn1.tianli0.top/npm/element-ui@2.15.6/lib/index.js"></script><script async src="https://cdn.bootcdn.net/ajax/libs/clipboard.js/2.0.11/clipboard.min.js"></script><script defer type="text/javascript" src="https://cdn1.tianli0.top/npm/sweetalert2@8.19.0/dist/sweetalert2.all.js"></script><script async src="//npm.elemecdn.com/pace-js@1.2.4/pace.min.js"></script><script defer src="https://cdn1.tianli0.top/gh/nextapps-de/winbox/dist/winbox.bundle.min.js"></script><script async src="//at.alicdn.com/t/c/font_3586335_hsivh70x0fm.js"></script><script async src="//at.alicdn.com/t/c/font_3636804_gr02jmjr3y9.js"></script><script async src="//at.alicdn.com/t/c/font_3612150_kfv55xn3u2g.js"></script><script async src="https://cdn.wpon.cn/2022-sucai/Gold-ingot.js"></script><canvas id="universe"></canvas><canvas id="snow"></canvas><script defer src="/js/fomal.js"></script><link rel="stylesheet" href="https://lf6-cdn-tos.bytecdntp.com/cdn/expire-1-M/aplayer/1.10.1/APlayer.min.css" media="print" onload="this.media='all'"><script src="https://lf6-cdn-tos.bytecdntp.com/cdn/expire-1-M/aplayer/1.10.1/APlayer.min.js"></script><script src="https://cdn1.tianli0.top/npm/js-heo@1.0.12/metingjs/Meting.min.js"></script><script src="https://lib.baomitu.com/pjax/0.2.8/pjax.min.js"></script><script>let pjaxSelectors = ["head > title","#config-diff","#body-wrap","#rightside-config-hide","#rightside-config-show","#web_bg",".js-pjax","#bibi","body > title","#app","#tag-echarts","#posts-echart","#categories-echarts"]

var pjax = new Pjax({
  elements: 'a:not([target="_blank"])',
  selectors: pjaxSelectors,
  cacheBust: false,
  analytics: false,
  scrollRestoration: false
})

document.addEventListener('pjax:send', function () {

  // removeEventListener scroll 
  window.tocScrollFn && window.removeEventListener('scroll', window.tocScrollFn)
  window.scrollCollect && window.removeEventListener('scroll', scrollCollect)

  typeof preloader === 'object' && preloader.initLoading()
  document.getElementById('rightside').style.cssText = "opacity: ''; transform: ''"
  
  if (window.aplayers) {
    for (let i = 0; i < window.aplayers.length; i++) {
      if (!window.aplayers[i].options.fixed) {
        window.aplayers[i].destroy()
      }
    }
  }

  typeof typed === 'object' && typed.destroy()

  //reset readmode
  const $bodyClassList = document.body.classList
  $bodyClassList.contains('read-mode') && $bodyClassList.remove('read-mode')

  typeof disqusjs === 'object' && disqusjs.destroy()
})

document.addEventListener('pjax:complete', function () {
  window.refreshFn()

  document.querySelectorAll('script[data-pjax]').forEach(item => {
    const newScript = document.createElement('script')
    const content = item.text || item.textContent || item.innerHTML || ""
    Array.from(item.attributes).forEach(attr => newScript.setAttribute(attr.name, attr.value))
    newScript.appendChild(document.createTextNode(content))
    item.parentNode.replaceChild(newScript, item)
  })

  GLOBAL_CONFIG.islazyload && window.lazyLoadInstance.update()

  typeof chatBtnFn === 'function' && chatBtnFn()
  typeof panguInit === 'function' && panguInit()

  // google analytics
  typeof gtag === 'function' && gtag('config', '', {'page_path': window.location.pathname});

  // baidu analytics
  typeof _hmt === 'object' && _hmt.push(['_trackPageview',window.location.pathname]);

  typeof loadMeting === 'function' && document.getElementsByClassName('aplayer').length && loadMeting()

  // prismjs
  typeof Prism === 'object' && Prism.highlightAll()

  typeof preloader === 'object' && preloader.endLoading()
})

document.addEventListener('pjax:error', (e) => {
  if (e.request.status === 404) {
    pjax.loadUrl('/404.html')
  }
})</script><script async data-pjax src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script></div><!-- hexo injector body_end start --> <script data-pjax>if(document.getElementById('recent-posts') && (location.pathname ==='/'|| '/' ==='all')){
    var parent = document.getElementById('recent-posts');
    var child = '<div class="recent-post-item" style="width:100%;height: auto"><div id="catalog_magnet"><div class="magnet_item"><a class="magnet_link" href="https://www.naste.top:1024/categories/Java/"><div class="magnet_link_context" style=""><span style="font-weight:500;flex:1">🍡 XiaoFeiのJava学习笔记 (55)</span><span style="padding:0px 4px;border-radius: 8px;"><i class="fas fa-arrow-circle-right"></i></span></div></a></div><div class="magnet_item"><a class="magnet_link" href="https://www.naste.top:1024/categories/Linux/"><div class="magnet_link_context" style=""><span style="font-weight:500;flex:1">🍼 XiaoFeiのLinux笔记 (33)</span><span style="padding:0px 4px;border-radius: 8px;"><i class="fas fa-arrow-circle-right"></i></span></div></a></div><div class="magnet_item"><a class="magnet_link" href="https://www.naste.top:1024/categories/Python/"><div class="magnet_link_context" style=""><span style="font-weight:500;flex:1">🍉 XiaoFeiのPython笔记 (2)</span><span style="padding:0px 4px;border-radius: 8px;"><i class="fas fa-arrow-circle-right"></i></span></div></a></div><div class="magnet_item"><a class="magnet_link" href="https://www.naste.top:1024/categories/前端/"><div class="magnet_link_context" style=""><span style="font-weight:500;flex:1">🍟 XiaoFeiの前端笔记 (17)</span><span style="padding:0px 4px;border-radius: 8px;"><i class="fas fa-arrow-circle-right"></i></span></div></a></div><div class="magnet_item"><a class="magnet_link" href="https://www.naste.top:1024/categories/Other/"><div class="magnet_link_context" style=""><span style="font-weight:500;flex:1">🍥 XiaoFeiのOther笔记 (10)</span><span style="padding:0px 4px;border-radius: 8px;"><i class="fas fa-arrow-circle-right"></i></span></div></a></div><div class="magnet_item" style="visibility: hidden"></div><a class="magnet_link_more"  href="https://www.naste.top:1024/categories/" style="flex:1;text-align: center;margin-bottom: 10px;">查看更多...</a></div></div>';
    console.log('已挂载magnet')
    parent.insertAdjacentHTML("afterbegin",child)}
     </script><style>#catalog_magnet{flex-wrap: wrap;display: flex;width:100%;justify-content:space-between;padding: 10px 10px 0 10px;align-content: flex-start;}.magnet_item{flex-basis: calc(33.333333333333336% - 5px);background: #e9e9e9;margin-bottom: 10px;border-radius: 8px;transition: all 0.2s ease-in-out;}.magnet_item:hover{background: var(--text-bg-hover)}.magnet_link_more{color:#555}.magnet_link{color:black}.magnet_link:hover{color:white}@media screen and (max-width: 600px) {.magnet_item {flex-basis: 100%;}}.magnet_link_context{display:flex;padding: 10px;font-size:16px;transition: all 0.2s ease-in-out;}.magnet_link_context:hover{padding: 10px 20px;}</style>
    <style></style><script data-pjax>
  function butterfly_swiper_injector_config(){
    var parent_div_git = document.getElementById('recent-posts');
    var item_html = '<div class="recent-post-item" style="height: auto;width: 100%"><div class="blog-slider swiper-container-fade swiper-container-horizontal" id="swiper_container"><div class="blog-slider__wrp swiper-wrapper" style="transition-duration: 0ms;"><div class="blog-slider__item swiper-slide" style="width: 750px; opacity: 1; transform: translate3d(0px, 0px, 0px); transition-duration: 0ms;"><a class="blog-slider__img" onclick="pjax.loadUrl(&quot;posts/204253054.html&quot;);" href="javascript:void(0);" alt=""><img width="48" height="48" src= "" data-lazy-src="https://www.naste.top:9000/webp/1675912191.webp" alt="" onerror="this.src=https://unpkg.zhimg.com/akilar-candyassets/image/loading.gif; this.onerror = null;"/></a><div class="blog-slider__content"><span class="blog-slider__code">2023-03-08</span><a class="blog-slider__title" onclick="pjax.loadUrl(&quot;posts/204253054.html&quot;);" href="javascript:void(0);" alt="">SpringBoot使用实现定时任务和xxl-job实现分布式任务调度</a><div class="blog-slider__text">SpringBoot使用实现定时任务和xxl-job实现分布式任务调度</div><a class="blog-slider__button" onclick="pjax.loadUrl(&quot;posts/204253054.html&quot;);" href="javascript:void(0);" alt="">详情       </a></div></div><div class="blog-slider__item swiper-slide" style="width: 750px; opacity: 1; transform: translate3d(0px, 0px, 0px); transition-duration: 0ms;"><a class="blog-slider__img" onclick="pjax.loadUrl(&quot;posts/538133837.html&quot;);" href="javascript:void(0);" alt=""><img width="48" height="48" src= "" data-lazy-src="https://www.naste.top:9000/webp/1678672933.webp" alt="" onerror="this.src=https://unpkg.zhimg.com/akilar-candyassets/image/loading.gif; this.onerror = null;"/></a><div class="blog-slider__content"><span class="blog-slider__code">2023-03-15</span><a class="blog-slider__title" onclick="pjax.loadUrl(&quot;posts/538133837.html&quot;);" href="javascript:void(0);" alt="">Hexo中Markdown语法与外挂标签写法汇总</a><div class="blog-slider__text">Hexo中Markdown语法与外挂标签写法汇总</div><a class="blog-slider__button" onclick="pjax.loadUrl(&quot;posts/538133837.html&quot;);" href="javascript:void(0);" alt="">详情       </a></div></div></div><div class="blog-slider__pagination swiper-pagination-clickable swiper-pagination-bullets"></div></div></div>';
    console.log('已挂载butterfly_swiper')
    parent_div_git.insertAdjacentHTML("afterbegin",item_html)
    }
  var elist = 'undefined'.split(',');
  var cpage = location.pathname;
  var epage = '/';
  var flag = 0;

  for (var i=0;i<elist.length;i++){
    if (cpage.includes(elist[i])){
      flag++;
    }
  }

  if ((epage ==='all')&&(flag == 0)){
    butterfly_swiper_injector_config();
  }
  else if (epage === cpage){
    butterfly_swiper_injector_config();
  }
  </script><script defer src="https://npm.elemecdn.com/hexo-butterfly-swiper/lib/swiper.min.js"></script><script defer data-pjax src="https://npm.elemecdn.com/hexo-butterfly-swiper/lib/swiper_init.js"></script><div class="js-pjax"><script async="async">var arr = document.getElementsByClassName('recent-post-item');
for(var i = 0;i<arr.length;i++){
    arr[i].classList.add('wow');
    arr[i].classList.add('animate__zoomIn');
    arr[i].setAttribute('data-wow-duration', '2s');
    arr[i].setAttribute('data-wow-delay', '200ms');
    arr[i].setAttribute('data-wow-offset', '30');
    arr[i].setAttribute('data-wow-iteration', '1');
  }</script><script async="async">var arr = document.getElementsByClassName('card-widget');
for(var i = 0;i<arr.length;i++){
    arr[i].classList.add('wow');
    arr[i].classList.add('animate__zoomIn');
    arr[i].setAttribute('data-wow-duration', '2s');
    arr[i].setAttribute('data-wow-delay', '200ms');
    arr[i].setAttribute('data-wow-offset', '30');
    arr[i].setAttribute('data-wow-iteration', '1');
  }</script></div><script defer src="https://npm.elemecdn.com/hexo-butterfly-wowjs/lib/wow.min.js"></script><script defer src="https://npm.elemecdn.com/hexo-butterfly-wowjs/lib/wow_init.js"></script><script data-pjax src="https://npm.elemecdn.com/hexo-filter-gitcalendar/lib/gitcalendar.js"></script><script data-pjax>
  function gitcalendar_injector_config(){
      var parent_div_git = document.getElementById('gitZone');
      var item_html = '<div class="recent-post-item" id="gitcalendarBar" style="width:100%;height:auto;padding:10px;"><style>#git_container{min-height: 320px}@media screen and (max-width:650px) {#git_container{min-height: 0px}}</style><div id="git_loading" style="width:10%;height:100%;margin:0 auto;display: block;"><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" viewBox="0 0 50 50" style="enable-background:new 0 0 50 50" xml:space="preserve"><path fill="#d0d0d0" d="M25.251,6.461c-10.318,0-18.683,8.365-18.683,18.683h4.068c0-8.071,6.543-14.615,14.615-14.615V6.461z" transform="rotate(275.098 25 25)"><animatetransform attributeType="xml" attributeName="transform" type="rotate" from="0 25 25" to="360 25 25" dur="0.6s" repeatCount="indefinite"></animatetransform></path></svg><style>#git_container{display: none;}</style></div><div id="git_container"></div></div>';
      parent_div_git.insertAdjacentHTML("afterbegin",item_html)
      console.log('已挂载gitcalendar')
      }

    if( document.getElementById('gitZone') && (location.pathname ==='/personal/about/'|| '/personal/about/' ==='all')){
        gitcalendar_injector_config()
        GitCalendarInit("https://gitcalendar.naste.top/api?xiao-i-fei",['#d9e0df', '#c6e0dc', '#a8dcd4', '#9adcd2', '#89ded1', '#77e0d0', '#5fdecb', '#47dcc6', '#39dcc3', '#1fdabe', '#00dab9'],'xiao-i-fei')
    }
  </script><!-- hexo injector body_end end --></body></html>